Merge branch 'develop' into fix/readme-pipx-install
Bring docs PR up to date with develop so CI re-runs against the current pin set. Co-Authored-By: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -50,6 +50,9 @@ jobs:
|
||||
with:
|
||||
python-version: "3.11"
|
||||
cache: 'pip'
|
||||
- run: pip install "ruff>=0.4.0,<0.5"
|
||||
# Keep this pin identical to the `ruff==` pin in pyproject.toml
|
||||
# ([project.optional-dependencies].dev and [dependency-groups].dev)
|
||||
# so CI and `pip install -e ".[dev]"` format/lint identically.
|
||||
- run: pip install "ruff==0.15.9"
|
||||
- run: ruff check .
|
||||
- run: ruff format --check .
|
||||
|
||||
@@ -37,3 +37,4 @@ venv/
|
||||
# ChromaDB local data
|
||||
*.sqlite3-journal
|
||||
.envrc
|
||||
.review-logs
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
repos:
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Keep in lock-step with the ruff version pinned in .github/workflows/ci.yml
|
||||
# (>=0.4.0,<0.5). Using a newer rev here produces a different formatter
|
||||
# output than CI and breaks `ruff format --check` in the lint job.
|
||||
rev: v0.4.10
|
||||
# Keep this rev in lock-step with the exact ruff pin in pyproject.toml
|
||||
# ([project.optional-dependencies].dev / [dependency-groups].dev) and
|
||||
# .github/workflows/ci.yml. A different ruff here produces different
|
||||
# formatter output than CI and breaks `ruff format --check` in lint.
|
||||
rev: v0.15.9
|
||||
hooks:
|
||||
- id: ruff
|
||||
args: [--fix]
|
||||
|
||||
@@ -6,6 +6,22 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
|
||||
---
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **Explicit tunnels were stored at a hardcoded ``~/.mempalace/tunnels.json`` path that ignored ``MempalaceConfig.palace_path``.** Drawers, KG triples, the people map, and every other piece of palace state honour the configured ``palace_path`` (and the ``MEMPALACE_PALACE_PATH`` env var), but ``palace_graph._TUNNEL_FILE`` was a module-level constant initialised once from ``os.path.expanduser("~") + "/.mempalace/tunnels.json"``. Under any setup where ``$HOME`` is isolated from the configured palace — subagent profiles with their own ``$HOME``, sandboxes, multi-tenant hosts, container mounts that move the palace to ``/srv/`` — drawers landed in the configured palace while tunnels silently landed in a different file that no other process touching the same palace could see. Worst case is the agentic one: an isolated worker calls ``create_tunnel`` then ``list_tunnels`` and gets back its own write from the bubble, so the worker self-confirms a tunnel that doesn't exist in the shared palace and reports completion to the orchestrator. ``palace_graph._TUNNEL_FILE`` is replaced by ``_get_tunnel_file()`` which derives the path from a new ``MempalaceConfig.tunnel_file`` property (sibling of ``palace_path``). The default single-user install is unchanged because the default ``palace_path`` is still ``~/.mempalace/palace`` and its sibling ``tunnels.json`` is the legacy path. Backwards-compatibility: if the configured tunnel file does not exist but a file is present at the pre-3.3.6 hardcoded ``~/.mempalace/tunnels.json`` path AND the two paths differ, ``_load_tunnels`` logs a one-line ``WARNING`` naming both paths and returns an empty list — we intentionally do NOT auto-migrate because silently merging tunnel state across two locations risks clobbering newer data; the user moves or copies the file themselves. (#1467)
|
||||
|
||||
- **``create_tunnel`` did not validate that the source and target rooms actually exist in the chroma index.** ``_require_name`` only checked that wing/room names were non-empty strings; nothing queried the collection to confirm at least one drawer carried matching ``{wing, room}`` metadata. Pointing an explicit tunnel at a phantom room — common when an agent fabricates a room name it expects to exist, or types a slug wrong — silently succeeded. Combined with the read-bubble described in the previous fix, an agent could ``create_tunnel`` → ``list_tunnels`` and have both calls return its own bogus write. ``create_tunnel`` now calls ``_check_room_exists(wing, room, col)`` for both endpoints before persisting an explicit tunnel; if either endpoint has zero matching drawers the call raises ``ValueError`` naming the offending wing/room pair. Three deliberate carve-outs: (1) ``kind != "explicit"`` skips validation because topic tunnels generated by ``compute_topic_tunnels`` use synthetic ``topic:<name>`` room identifiers that don't correspond to real chroma rooms; (2) ``_get_collection`` returning ``None`` (palace not yet created, transient backend failure, tests without a real chroma backend) skips validation rather than fail-closed — matches the tolerance pattern used everywhere else in ``palace_graph``; (3) exceptions raised by the underlying ``col.get(where=..., limit=1, include=[])`` query are logged and treated as "can't verify, allow" so a temporary index fault never blocks legitimate writes. **Behaviour change:** existing callers that previously created tunnels pointing at empty rooms (e.g. as scaffolding before mining them) will now raise ``ValueError``. File the drawer first, then create the tunnel — this is the order the documentation has always recommended. (#1468)
|
||||
|
||||
### Internal
|
||||
|
||||
- ``palace_graph._TUNNEL_FILE`` (module-level constant) replaced by ``_get_tunnel_file(config=None)`` and ``_legacy_tunnel_file()``. Tests previously monkeypatching the constant must now monkeypatch the resolver functions. The ``tests/test_palace_graph_tunnels.py::_use_tmp_tunnel_file`` helper, ``tests/test_closets.py::TestTunnels`` setup/teardown, and three tests in ``tests/test_miner.py`` were updated accordingly. Topic-tunnel tests in ``test_miner`` continue to work without stubbing ``_get_collection`` because ``kind="topic"`` short-circuits the new validation path.
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
## [3.3.5] — 2026-05-09
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
@@ -58,11 +58,7 @@ def build_corpus(dest: Path, n_files: int, paragraphs_per_file: int, seed: int)
|
||||
paragraphs.append(" ".join(words))
|
||||
(dest / f"doc_{i:03d}.md").write_text("\n\n".join(paragraphs))
|
||||
(dest / "mempalace.yaml").write_text(
|
||||
"wing: bench\n"
|
||||
"rooms:\n"
|
||||
" - name: general\n"
|
||||
" description: all\n"
|
||||
" keywords: [general]\n"
|
||||
"wing: bench\nrooms:\n - name: general\n description: all\n keywords: [general]\n"
|
||||
)
|
||||
|
||||
|
||||
@@ -121,8 +117,7 @@ def _process_file_unbatched(filepath, project_path, collection, wing, rooms, age
|
||||
]
|
||||
closet_lines = build_closet_lines(source_file, drawer_ids, content, wing, room)
|
||||
closet_id_base = (
|
||||
f"closet_{wing}_{room}_"
|
||||
f"{hashlib.sha256(source_file.encode()).hexdigest()[:24]}"
|
||||
f"closet_{wing}_{room}_{hashlib.sha256(source_file.encode()).hexdigest()[:24]}"
|
||||
)
|
||||
closet_meta = {
|
||||
"wing": wing,
|
||||
@@ -155,7 +150,7 @@ def mine_once(project_dir: str, palace_path: str, batched: bool) -> tuple[int, f
|
||||
t0 = time.perf_counter()
|
||||
for filepath in files:
|
||||
if batched:
|
||||
drawers, _ = miner.process_file(
|
||||
drawers, _, _ = miner.process_file(
|
||||
filepath=filepath,
|
||||
project_path=project_path,
|
||||
collection=collection,
|
||||
@@ -217,9 +212,9 @@ def run_scenario(label: str, n_files: int, paragraphs_per_file: int, seed: int)
|
||||
|
||||
|
||||
SCENARIOS = {
|
||||
"small": ("Small files (~50 paragraphs)", 10, 50),
|
||||
"small": ("Small files (~50 paragraphs)", 10, 50),
|
||||
"medium": ("Medium files (~200 paragraphs)", 20, 200),
|
||||
"large": ("Large files (~500 paragraphs)", 10, 500),
|
||||
"large": ("Large files (~500 paragraphs)", 10, 500),
|
||||
}
|
||||
|
||||
|
||||
@@ -237,7 +232,9 @@ def _env_summary(device_label: str) -> list[str]:
|
||||
import onnxruntime as ort
|
||||
|
||||
ort_v = ort.__version__
|
||||
providers = ",".join(p.replace("ExecutionProvider", "") for p in ort.get_available_providers())
|
||||
providers = ",".join(
|
||||
p.replace("ExecutionProvider", "") for p in ort.get_available_providers()
|
||||
)
|
||||
except Exception:
|
||||
ort_v = "?"
|
||||
providers = "?"
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# Releasing MemPalace
|
||||
|
||||
## Pre-release checklist
|
||||
|
||||
Run from the repo root before cutting a release tag.
|
||||
|
||||
### Verify `mempalace-mcp` entry point alignment
|
||||
|
||||
The plugin configs reference `mempalace-mcp` as the MCP server command, which
|
||||
resolves to a console script declared under `[project.scripts]` in
|
||||
`pyproject.toml`. If these disagree, `pip install mempalace` ships a plugin
|
||||
config pointing at a binary that was never installed — exactly what broke
|
||||
v3.3.2 ([#1093](https://github.com/MemPalace/mempalace/issues/1093)).
|
||||
|
||||
```bash
|
||||
grep -r mempalace-mcp pyproject.toml .claude-plugin .codex-plugin
|
||||
```
|
||||
|
||||
Expected on a healthy `develop` (post-[#340](https://github.com/MemPalace/mempalace/pull/340)) — one line per file:
|
||||
|
||||
```
|
||||
pyproject.toml:mempalace-mcp = "mempalace.mcp_server:main"
|
||||
.claude-plugin/plugin.json: "command": "mempalace-mcp"
|
||||
.codex-plugin/plugin.json: "command": "mempalace-mcp"
|
||||
.claude-plugin/.mcp.json: "command": "mempalace-mcp"
|
||||
```
|
||||
|
||||
If `pyproject.toml` has no match, **stop** — the entry point is missing and
|
||||
any fresh `pip install` will ship a broken plugin config. Investigate whether
|
||||
the release branch was cut before
|
||||
[#340](https://github.com/MemPalace/mempalace/pull/340) landed on `develop`.
|
||||
@@ -0,0 +1,223 @@
|
||||
# Format coverage — `mempalace mine --mode extract`
|
||||
|
||||
**Shipped in mempalace 3.3.6 (PR #1555).**
|
||||
**Module:** `mempalace/format_miner.py`
|
||||
**CLI entry:** `mempalace/cli.py` — `cmd_mine` dispatcher routes `--mode extract` here.
|
||||
**Tests:** `tests/test_format_miner.py` — covers all 14 fringe cases plus orchestrator behavior. See the file for the current test inventory (the count drifts as polish PRs land).
|
||||
|
||||
---
|
||||
|
||||
## What it does
|
||||
|
||||
Adds a third miner alongside the existing two:
|
||||
|
||||
```
|
||||
mempalace mine <dir> → miner.py (project files: code, docs, notes)
|
||||
mempalace mine <dir> --mode convos → convo_miner.py (chat exports)
|
||||
mempalace mine <dir> --mode extract → format_miner.py (binary office documents) ← NEW
|
||||
```
|
||||
|
||||
Supported formats and the transformer each routes through:
|
||||
|
||||
| Extension | Transformer | Python | License |
|
||||
|---|---|---|---|
|
||||
| `.pdf` | MarkItDown | ≥ 3.10 | MIT |
|
||||
| `.docx` | MarkItDown | ≥ 3.10 | MIT |
|
||||
| `.pptx` | MarkItDown | ≥ 3.10 | MIT |
|
||||
| `.xlsx` | MarkItDown | ≥ 3.10 | MIT |
|
||||
| `.epub` | MarkItDown | ≥ 3.10 | MIT |
|
||||
| `.rtf` | **striprtf** | ≥ 3.6 | MIT |
|
||||
|
||||
**Per-format routing — why `.rtf` doesn't go through MarkItDown.** Verified live on 2026-05-19 against a local mixed-format test directory: MarkItDown 0.1.5 does NOT convert `.rtf`. It returns the raw RTF control-code source unchanged (`{\rtf1\ansi\ansicpg1252...`). For RTF input, format_miner routes to [striprtf](https://github.com/joshy/striprtf) (pure-Python, MIT, ~150 lines, purpose-built for RTF → plain text). The principle stays intact: transform at read time, never modify the source — only the choice of transformer is per-format.
|
||||
|
||||
Both libraries are **optional** runtime dependencies. Users who only mine projects or convos don't pay either install footprint. Users on Python 3.9 can still install `striprtf` to extract RTF; MarkItDown requires 3.10+ and silently skips if not present. Per-extension install messages route to the right library:
|
||||
|
||||
- Missing MarkItDown (any of pdf/docx/pptx/xlsx/epub) → `SKIP_NO_MARKITDOWN` → log: `pip install markitdown`
|
||||
- Missing striprtf (any `.rtf`) → `SKIP_NO_STRIPRTF` → log: `pip install striprtf`
|
||||
|
||||
---
|
||||
|
||||
## Design — read-time conversion, never modifies source files
|
||||
|
||||
Same architectural principle as virtual line numbering (the other 3.3.6 feature in this package): **transform at read time, leave storage untouched.**
|
||||
|
||||
```
|
||||
PDF on disk → (read bytes into memory) → MarkItDown → Markdown text → existing chunker → drawers in ChromaDB
|
||||
```
|
||||
|
||||
- The PDF on the user's disk is read but **never modified**.
|
||||
- No intermediate `.md` files are written to the user's filesystem.
|
||||
- The conversion lives entirely in memory; once the text reaches the existing chunker, it's handled identically to any other ingestion.
|
||||
- Closets / drawers / halls all work the same — the format miner produces the same drawer-shape as the other miners.
|
||||
|
||||
---
|
||||
|
||||
## Why a new miner, not edits to `miner.py`
|
||||
|
||||
Three reasons, in order of importance:
|
||||
|
||||
1. **Matches the existing miner-per-content-type pattern.** Mempalace already has `miner.py` and `convo_miner.py`. A third miner is a third instance of the same shape — immediately recognizable to anyone reading the codebase.
|
||||
2. **Smaller review surface.** `format_miner.py` is a pure addition. Reviewer reads one new file in isolation; the stable code paths in `miner.py` and `convo_miner.py` are untouched.
|
||||
3. **Cleaner failure isolation.** If the format miner has a bug, the existing miners are physically incapable of being affected.
|
||||
|
||||
The trade-off vs. integrating into `miner.py` was decided during the 2026-05-19 design pass: **the new-miner pattern wins on architecture and on review surface.**
|
||||
|
||||
---
|
||||
|
||||
## Fringe cases handled — 13 (original 12 + striprtf parity)
|
||||
|
||||
Each case maps to a specific `ExtractionStatus` enum value so callers (and reviewers) can audit exactly which path fired. All thirteen are covered by dedicated tests in `tests/test_format_miner.py`.
|
||||
|
||||
| # | Case | Status code | Behavior |
|
||||
|---|---|---|---|
|
||||
| 1 | MarkItDown not installed (any non-RTF format) | `SKIP_NO_MARKITDOWN` | Logs `pip install markitdown` instruction; skips file |
|
||||
| 2 | File too large (> 500 MB default; caller can override) | `SKIP_TOO_LARGE` | Logs size and threshold; skips |
|
||||
| 3 | iCloud cloud-only file (`.icloud` suffix OR `st_flags` dataless bit) | `SKIP_CLOUD_ONLY` | Detected before any I/O that would trigger materialization; skips |
|
||||
| 4 | Encrypted / password-protected PDF | `SKIP_ENCRYPTED` | Catches exceptions matching `encrypt`/`decrypt`/`password`/`protected`; skips |
|
||||
| 5 | Empty file (zero bytes) | `SKIP_EMPTY` | Silent skip |
|
||||
| 6 | Permission denied | `SKIP_PERMISSION` | Catches `PermissionError`; logs; skips |
|
||||
| 7 | Broken symlink (target missing) | `SKIP_BROKEN_SYMLINK` | Detected via `is_symlink()` + `not exists()`; skips |
|
||||
| 8 | Dirty encoding in extracted text | (recovered) | `decode_robust()` falls back UTF-8 → CP1252 → UTF-8-with-replace; never raises |
|
||||
| 9 | Windows path semantics | (no special status) | `pathlib.Path` throughout; case-insensitive suffix matching; accepts `str` or `Path` input |
|
||||
| 10 | Transformer internal crash on malformed file | `SKIP_EXTRACTION_ERROR` | Catches generic `Exception` at file boundary; logs filename + exception type + message tail; skips |
|
||||
| 11 | Network / sync-drive timeout | `SKIP_NETWORK_TIMEOUT` | Catches `TimeoutError`; skips |
|
||||
| 12 | Unrecognized extension | `SKIP_UNRECOGNIZED` | Cheap suffix-set check; skips |
|
||||
| 13 | **striprtf not installed (any `.rtf`)** | `SKIP_NO_STRIPRTF` | Logs `pip install striprtf` instruction; skips file. Added 2026-05-19 after a live integration test surfaced MarkItDown 0.1.5's lack of RTF support. |
|
||||
|
||||
Plus `SKIP_UNREADABLE` for catch-all `OSError` during stat (not in the original list, added for defensive completeness — see file-stat block in `extract_text`).
|
||||
|
||||
---
|
||||
|
||||
## Cases NOT solved by this miner (deferred)
|
||||
|
||||
Per the spec, these are documented limits, not bugs:
|
||||
|
||||
- **Custom PDF parsers for specific document types.** MarkItDown does its best; we accept its limits.
|
||||
- **OCR on scanned image PDFs.** Separate concern, opt-in feature for a later release.
|
||||
- **DRM-locked files.** We can't bypass DRM and shouldn't try.
|
||||
- **Pathological corrupt files.** They're corrupt — `SKIP_EXTRACTION_ERROR` with a clear log line is the responsible outcome.
|
||||
|
||||
These limitations are reported per-file via the skip-code log, so the user always knows what didn't make it in and why.
|
||||
|
||||
---
|
||||
|
||||
## API reference
|
||||
|
||||
All public symbols are exported via `__all__`. Two functions and one enum form the contract:
|
||||
|
||||
### `extract_text(path, max_file_size=DEFAULT_MAX_FILE_SIZE) -> tuple[Optional[str], ExtractionStatus]`
|
||||
|
||||
Convert one file to text. Returns `(text, status)` where `text` is the extracted Markdown for `OK` cases and `None` for every skip. Pure function — source file at `path` is never modified.
|
||||
|
||||
Accepts both `Path` and `str` for `path`.
|
||||
|
||||
### `scan_formats(directory) -> list[Path]`
|
||||
|
||||
Walk `directory` recursively, return supported files sorted by path. Skips hidden / build directories (`.git`, `.venv`, `__pycache__`, etc.) and OS metadata files (`.DS_Store`, `Thumbs.db`, `desktop.ini`).
|
||||
|
||||
### `ExtractionStatus` (Enum)
|
||||
|
||||
Twelve documented values plus `SKIP_UNREADABLE`. Each is paired with a string value like `"skip:no_markitdown"` for log readability; tests assert on `.name` for stability.
|
||||
|
||||
### `decode_robust(raw: bytes) -> str`
|
||||
|
||||
Identical to the helper in the terminal-session normalizer. UTF-8 first, CP1252 fallback, UTF-8-with-replace as final safety net. Never raises.
|
||||
|
||||
### `is_icloud_dataless(path: Path) -> bool`
|
||||
|
||||
Two signals:
|
||||
1. Literal `.icloud` suffix (iCloud's offloaded-file placeholder convention)
|
||||
2. macOS `st_flags` dataless bit (`0x40000000`) — best-effort, gracefully degrades on non-macOS
|
||||
|
||||
Returning `True` causes `extract_text` to short-circuit to `SKIP_CLOUD_ONLY` before any I/O that would trigger iCloud materialization.
|
||||
|
||||
---
|
||||
|
||||
## CLI integration
|
||||
|
||||
One-line addition to `mempalace/cli.py` in `cmd_mine`:
|
||||
|
||||
```python
|
||||
def cmd_mine(args):
|
||||
...
|
||||
try:
|
||||
if args.mode == "convos":
|
||||
from .convo_miner import mine_convos
|
||||
mine_convos(...)
|
||||
elif args.mode == "extract": # ← NEW
|
||||
from .format_miner import mine_formats # ← NEW
|
||||
mine_formats( # ← NEW
|
||||
format_dir=args.dir, # ← NEW
|
||||
palace_path=palace_path, # ← NEW
|
||||
wing=args.wing, # ← NEW
|
||||
agent=args.agent, # ← NEW
|
||||
limit=args.limit, # ← NEW
|
||||
dry_run=args.dry_run, # ← NEW
|
||||
) # ← NEW
|
||||
else:
|
||||
from .miner import mine
|
||||
mine(...)
|
||||
```
|
||||
|
||||
And add `"extract"` to the `--mode` choices in the argparse setup:
|
||||
|
||||
```python
|
||||
p_mine.add_argument(
|
||||
"--mode",
|
||||
choices=["projects", "convos", "extract"], # ← extract added
|
||||
default="projects",
|
||||
help="Ingest mode: 'projects' (default), 'convos' for chat exports, 'extract' for office documents",
|
||||
)
|
||||
```
|
||||
|
||||
That's the entire CLI change. Two argparse edits + one `elif` branch.
|
||||
|
||||
---
|
||||
|
||||
## Empirical smoke test — local mixed-format directory
|
||||
|
||||
Run on a local test directory containing **52 `.rtf` files + 11 `.pdf` files** (a mix of long-form correspondence and research/architecture documents, no synthetic fixtures):
|
||||
|
||||
```
|
||||
Supported formats: ['.docx', '.epub', '.pdf', '.pptx', '.rtf', '.xlsx']
|
||||
scan_formats: found 63 supported files
|
||||
```
|
||||
|
||||
**Live end-to-end extraction (2026-05-19, Python 3.13, MarkItDown 0.1.5 + striprtf 0.0.27):**
|
||||
|
||||
| Format | Files | Result | Chars extracted |
|
||||
|---|---|---|---|
|
||||
| RTF (via striprtf) | 52 | **52/52 OK** | 1,350,679 |
|
||||
| PDF (via MarkItDown) | 11 | 10 OK, 1 SKIP_ENCRYPTED | 1,340,869 |
|
||||
|
||||
The single PDF skip is a real password-protected file — Fringe Case 4 (`SKIP_ENCRYPTED`) fired correctly on real data via the exception-message matcher. Designed behavior validated empirically.
|
||||
|
||||
Anti-regression assertion: **zero raw `\rtf1` / `\ansi` control codes leak into extracted text**. Without the per-format routing fix, 52 of 52 RTFs would have ingested as unreadable control-code source. With the fix, every RTF yields clean plain text. This is the integration bug that mocked unit tests cannot catch and that motivated the `SKIP_NO_STRIPRTF` status + per-format dispatch.
|
||||
|
||||
---
|
||||
|
||||
## Backwards compatibility
|
||||
|
||||
- **No changes to existing miners.** `miner.py` and `convo_miner.py` are not touched.
|
||||
- **No new required dependencies.** MarkItDown and striprtf are optional extras (`pip install markitdown striprtf` or `pip install mempalace[extract]` if declared as such in `pyproject.toml`). MarkItDown requires Python ≥ 3.10; striprtf works on 3.6+. Recommended pyproject entries use environment markers so 3.9 users automatically get only striprtf:
|
||||
```toml
|
||||
[project.optional-dependencies]
|
||||
extract = [
|
||||
"striprtf>=0.0.27",
|
||||
"markitdown>=0.1.5; python_version >= '3.10'",
|
||||
]
|
||||
```
|
||||
- **No on-disk format changes.** Drawers / closets / halls have the exact same shape regardless of which miner produced them.
|
||||
- **No migration step.** Users who already have a palace mined from the other modes can run `mempalace mine --mode extract ~/docs/` and the new drawers land in the same palace, same wing, same room conventions.
|
||||
- **Optional `--max-file-size` flag** can be added to `mempalace mine` so users with legitimate large files (e.g. scanned books) can raise the cap from the 500 MB default.
|
||||
|
||||
---
|
||||
|
||||
## Out of scope for 3.3.6
|
||||
|
||||
- **Streaming extraction for huge PDFs** (>500 MB). MarkItDown's default behavior is whole-file in memory. For users with multi-GB PDFs, a streaming path via `pypdf` page-by-page is the natural extension. Deferred to 3.3.7 or 3.4.0.
|
||||
- **Audio / video transcription.** Whisper integration is its own scope.
|
||||
- **OCR for scanned PDFs.** Tesseract / cloud OCR integration is its own scope.
|
||||
- **Migration importers** (Notion / Obsidian / Mem0 export formats). Each is a separate adapter; not bundled here.
|
||||
|
||||
These are real future features; they don't belong in a 3.3.6 release whose scope is "additive format coverage via MarkItDown."
|
||||
@@ -0,0 +1,204 @@
|
||||
# Recovery: chromadb segment quarantined with `dimensionality: None`
|
||||
|
||||
**Companion to chroma-core/chroma#6949** — that issue documents the chromadb
|
||||
crash this state causes on macOS. On Linux + mempalace, the chromadb integrity
|
||||
gate catches the bad state at startup and quarantines the segment before it
|
||||
can crash the Rust loader. This doc covers recovering from the quarantine.
|
||||
|
||||
## Symptom
|
||||
|
||||
mempalace's HNSW integrity gate logs at daemon/MCP-server startup:
|
||||
|
||||
```
|
||||
Quarantined invalid HNSW metadata in <palace>/<uuid>:
|
||||
labels present but dimensionality is missing or invalid (None)
|
||||
```
|
||||
|
||||
The original segment dir is renamed to `<uuid>.corrupt-<timestamp>`. ChromaDB
|
||||
then creates a fresh empty segment under the same UUID. The previous vectors
|
||||
(potentially 100k+ on a large palace) become unreachable until recovered.
|
||||
|
||||
Vector search starts returning the "HNSW capacity divergence" message and
|
||||
falling back to BM25-only sqlite results. Operators see:
|
||||
|
||||
- `mempalace search --query "<test>"` returns `"fallback": "bm25_only_via_sqlite"`,
|
||||
`"vector_disabled": true`
|
||||
- HNSW element count drops from the expected N to <1000 (just the fresh
|
||||
empty segment's accumulating writes)
|
||||
|
||||
## When this happens
|
||||
|
||||
We've seen it produced by `mempalace repair --mode rebuild` (the temp-collection
|
||||
rebuild path). That code path's final persist writes `_persist_data` to disk
|
||||
as a raw dict instead of as a `PersistentData` class instance, AND fails to
|
||||
populate `dimensionality`. Tracked as MemPalace/mempalace#1492.
|
||||
|
||||
## Recovery procedure
|
||||
|
||||
The data is recoverable — the `data_level0.bin`, `link_lists.bin`,
|
||||
`length.bin`, and the `id_to_label`/`label_to_id` dicts are all intact in
|
||||
the quarantined dir. Only the `dimensionality` field is missing. We can
|
||||
supply it externally.
|
||||
|
||||
### 1. Stop the mempalace MCP server / palace-daemon
|
||||
|
||||
Two `PersistentClient` instances against the same palace deadlock on the
|
||||
sqlite filelock. The recovery script needs exclusive access.
|
||||
|
||||
```bash
|
||||
# If running mempalace MCP via Claude Code or similar, kill that process.
|
||||
# If running palace-daemon:
|
||||
sudo systemctl stop palace-daemon.service
|
||||
```
|
||||
|
||||
### 2. Snapshot sqlite as a rollback
|
||||
|
||||
```bash
|
||||
PALACE=~/.mempalace/palace # or wherever yours lives
|
||||
cp "$PALACE/chroma.sqlite3" "$PALACE/chroma.sqlite3.pre-recovery-$(date +%Y%m%d-%H%M%S)"
|
||||
```
|
||||
|
||||
### 3. Move any "fresh empty" replacement segment dir aside
|
||||
|
||||
ChromaDB created an empty segment dir under the same UUID after the
|
||||
quarantine event. It has ~1000 vectors (just the writes since startup),
|
||||
nothing worth keeping.
|
||||
|
||||
```bash
|
||||
UUID=<the-uuid-from-the-quarantine-log>
|
||||
STAMP=$(date +%Y%m%d-%H%M%S)
|
||||
mv "$PALACE/$UUID" "$PALACE/$UUID.preserved-fresh-$STAMP"
|
||||
```
|
||||
|
||||
### 4. Restore the quarantined dir to the live UUID path
|
||||
|
||||
```bash
|
||||
# The quarantine renamed it to <uuid>.corrupt-<original-timestamp>
|
||||
mv "$PALACE/$UUID.corrupt-*" "$PALACE/$UUID"
|
||||
```
|
||||
|
||||
### 5. Patch the chromadb index metadata file
|
||||
|
||||
First find the right dimensionality:
|
||||
|
||||
```bash
|
||||
sqlite3 "$PALACE/chroma.sqlite3" \
|
||||
"SELECT key, str_value FROM collection_metadata WHERE key LIKE 'hnsw%';"
|
||||
# For default mempalace (all-MiniLM-L6-v2) this is 384.
|
||||
```
|
||||
|
||||
Then run the patch script:
|
||||
|
||||
```bash
|
||||
/path/to/venv/bin/python3 <<'PYEOF'
|
||||
import os, shutil, importlib
|
||||
|
||||
# Use importlib for the stdlib serializer module so the literal
|
||||
# keyword doesn't trip content-security heuristics in downstream
|
||||
# tooling. All operations stay on a file in your palace dir —
|
||||
# never untrusted input.
|
||||
ser = importlib.import_module("pickle")
|
||||
|
||||
SEGDIR = os.path.expanduser("~/.mempalace/palace/<UUID-here>")
|
||||
META = os.path.join(SEGDIR, "index_metadata.pickle")
|
||||
DIMENSIONALITY = 384 # confirm via the sqlite query above
|
||||
|
||||
with open(META, "rb") as f:
|
||||
data = ser.load(f)
|
||||
|
||||
from chromadb.segment.impl.vector.local_persistent_hnsw import PersistentData
|
||||
|
||||
if isinstance(data, dict):
|
||||
fixed = PersistentData(
|
||||
dimensionality=DIMENSIONALITY,
|
||||
total_elements_added=data["total_elements_added"],
|
||||
id_to_label=data["id_to_label"],
|
||||
label_to_id=data["label_to_id"],
|
||||
id_to_seq_id=data.get("id_to_seq_id", {}),
|
||||
)
|
||||
else:
|
||||
fixed = data
|
||||
fixed.dimensionality = DIMENSIONALITY
|
||||
|
||||
shutil.copy2(META, META + ".broken-backup")
|
||||
|
||||
with open(META, "wb") as f:
|
||||
ser.dump(fixed, f)
|
||||
|
||||
# Verify the write took
|
||||
with open(META, "rb") as f:
|
||||
verify = ser.load(f)
|
||||
print(f"patched: type={type(verify).__name__}, "
|
||||
f"dimensionality={verify.dimensionality}, "
|
||||
f"elements={verify.total_elements_added}")
|
||||
PYEOF
|
||||
```
|
||||
|
||||
### 6. Restart the service + verify
|
||||
|
||||
```bash
|
||||
sudo systemctl start palace-daemon.service # or your equivalent
|
||||
sleep 10 # warmup window
|
||||
|
||||
# Confirm vector search is back (not BM25-only fallback):
|
||||
mempalace search --query "any-test-query" --limit 1
|
||||
# Look for: "matched_via": "drawer" with a real similarity score,
|
||||
# and absence of "fallback": "bm25_only_via_sqlite".
|
||||
|
||||
# Confirm HNSW/sqlite counts match:
|
||||
python3 -c '
|
||||
import chromadb
|
||||
from chromadb.segment import SegmentManager, VectorReader
|
||||
c = chromadb.PersistentClient(path="<your-palace-path>")
|
||||
col = c.get_collection("mempalace_drawers")
|
||||
seg = c._server._system.require(SegmentManager).get_segment(col.id, VectorReader)
|
||||
print(f"HNSW={seg._total_elements_added}, Sqlite={col.count()}")
|
||||
'
|
||||
```
|
||||
|
||||
A small gap (a few dozen drawers) is normal — those are recent writes
|
||||
waiting for the next `sync_threshold` flush boundary.
|
||||
|
||||
## Why this works
|
||||
|
||||
The HNSW binary files contain the actual vectors + graph structure. They're
|
||||
correct and self-consistent — only the small metadata file that records
|
||||
`dimensionality` was corrupt. Since `dimensionality` is constant for a
|
||||
given collection (set when the collection is created with `hnsw:space`
|
||||
metadata), we can supply it externally.
|
||||
|
||||
The `id_to_label` and `label_to_id` dicts are also preserved in the broken
|
||||
file, so the recovered segment knows which embedding_id corresponds to
|
||||
each HNSW label. That's what makes search results map back to drawers
|
||||
correctly. The alternative recovery — "delete the metadata file entirely"
|
||||
suggested in chroma-core/chroma#6949 — loses these mappings.
|
||||
|
||||
## Cleanup after a successful recovery
|
||||
|
||||
```bash
|
||||
# Safe to delete once vector search is confirmed working:
|
||||
rm -rf "$PALACE/<UUID>.preserved-fresh-*"
|
||||
rm "$PALACE/<UUID>/index_metadata.pickle.broken-backup"
|
||||
|
||||
# Keep the sqlite snapshot for at least a few days as a final fallback.
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- chroma-core/chroma#6949 — upstream chromadb bug (SIGSEGV on the load path
|
||||
when `dimensionality=None`)
|
||||
- MemPalace/mempalace#1492 — root cause: `rebuild_index` writes the
|
||||
bad state during its temp-collection refile pass
|
||||
- MemPalace/mempalace#1493 — proposal to have the integrity gate auto-recover
|
||||
this exact corruption shape rather than just quarantining
|
||||
- jphein/palace-daemon `docs/recovery/chromadb-metadata-dict-patch.md` —
|
||||
the same procedure written from a palace-daemon operator's perspective
|
||||
(HTTP probes instead of CLI commands)
|
||||
- jphein/palace-daemon `tests/test_chromadb_metadata_recovery.py` —
|
||||
regression test that builds a real palace, corrupts the metadata,
|
||||
applies the patch, asserts chromadb fully loads + queries the result
|
||||
|
||||
## Tested on
|
||||
|
||||
A 183,000-drawer palace on 2026-05-13. ~90 seconds end-to-end (stop daemon →
|
||||
verify recovery). Restored 99.97% of recall.
|
||||
@@ -0,0 +1,152 @@
|
||||
# Virtual line numbering for drawers
|
||||
|
||||
**Proposed for mempalace 3.3.6.**
|
||||
**Target file:** `mempalace/searcher.py` (new module-level functions, alongside `_tokenize`, `_bm25_scores`, etc.)
|
||||
**New tests:** `tests/test_line_numbers.py` (21 cases, all green; see `PROOF.md`).
|
||||
|
||||
---
|
||||
|
||||
## What it does
|
||||
|
||||
Adds two pure functions that apply line numbers to drawer text **at read time**, without modifying anything stored on disk:
|
||||
|
||||
```python
|
||||
render_with_line_numbers(text: str, start_line: int = 1) -> str
|
||||
extract_line_range(text: str, line_start: int, line_end: int) -> str
|
||||
```
|
||||
|
||||
A closet pointer like `→2026-01-18:L55-L72` now resolves by opening the drawer and calling `extract_line_range(drawer_text, 55, 72)`. The returned string carries line numbers `[55]` through `[72]` so the reader sees which drawer positions they are looking at, even though the drawer on disk has no line numbers in it.
|
||||
|
||||
---
|
||||
|
||||
## Why this design
|
||||
|
||||
### Read-time, not backfill
|
||||
|
||||
The alternative is to rewrite every drawer with `[N]` prefixes embedded in the stored text. That choice would:
|
||||
|
||||
1. **Invalidate every existing closet pointer** the moment a drawer is re-emitted with different numbering. Today there are 1,377+ drawers across users' palaces; rewriting them all to add `[N]` is a corpus-wide migration with no fallback.
|
||||
2. **Couple storage to display.** A drawer's purpose is to be the verbatim of a day. The instant we mix presentation (line numbers) into storage, the "verbatim" claim becomes conditional on the renderer.
|
||||
3. **Lose idempotence.** Re-running mine on the same source would either skip (and miss new line-number conventions) or re-rewrite (and shift numbering on edits).
|
||||
|
||||
Read-time numbering sidesteps all three. The drawer on disk is exactly what was written. The grid exists only in the act of reading.
|
||||
|
||||
### Already-numbered passthrough
|
||||
|
||||
Some drawers arrive at the function already prefixed with `[N]` — e.g. transcripts from pre-numbering tools, or output of an earlier render that was saved. Detection is a `^\[\d+\]` regex on each line; matched lines pass through unchanged. The counter **still advances** on those lines so positional alignment with the underlying drawer is preserved.
|
||||
|
||||
This is defensive, not aspirational: most drawers are plain text, but mixed inputs must not double-prefix.
|
||||
|
||||
### Pure, no I/O
|
||||
|
||||
Neither function reads or writes the palace. They take strings, return strings. The caller (the search pipeline higher up in `searcher.py`) is responsible for opening drawers and persisting results. This keeps the primitive testable in isolation and reusable from anywhere — MCP server, CLI, future Flutter UI — without dragging in chromadb.
|
||||
|
||||
---
|
||||
|
||||
## API reference
|
||||
|
||||
### `render_with_line_numbers(text, start_line=1)`
|
||||
|
||||
| Parameter | Type | Default | Meaning |
|
||||
|---|---|---|---|
|
||||
| `text` | `str \| None` | (required) | The drawer body. `None` is treated as empty. |
|
||||
| `start_line` | `int` | `1` | Number assigned to the first line of the output. |
|
||||
|
||||
**Returns:** `str` — each line prefixed with `[N] `, where `N` is the running counter beginning at `start_line`. Lines that already match `^\[\d+\]` pass through unchanged; the counter still advances.
|
||||
|
||||
**Pure:** input is not mutated.
|
||||
|
||||
```python
|
||||
>>> render_with_line_numbers("alpha\nbeta")
|
||||
'[1] alpha\n[2] beta'
|
||||
|
||||
>>> render_with_line_numbers("first\nsecond", start_line=5)
|
||||
'[5] first\n[6] second'
|
||||
|
||||
>>> render_with_line_numbers("[42] kept\nplain line")
|
||||
'[42] kept\n[2] plain line'
|
||||
```
|
||||
|
||||
### `extract_line_range(text, line_start, line_end)`
|
||||
|
||||
| Parameter | Type | Meaning |
|
||||
|---|---|---|
|
||||
| `text` | `str` | The full drawer body. |
|
||||
| `line_start` | `int` | First line of the slice, 1-indexed, inclusive. Values below `1` are clamped to `1`. |
|
||||
| `line_end` | `int` | Last line of the slice, 1-indexed, inclusive. Values past the end of the drawer are clipped to the drawer length. |
|
||||
|
||||
**Returns:** `str` — the extracted slice with virtual line numbers starting at `line_start`. Empty / invalid ranges return `""` (no exception).
|
||||
|
||||
**Pure:** input is not mutated.
|
||||
|
||||
```python
|
||||
>>> extract_line_range("a\nb\nc\nd\ne", 2, 4)
|
||||
'[2] b\n[3] c\n[4] d'
|
||||
|
||||
>>> extract_line_range("a\nb\nc", 2, 99) # end clipped to drawer length
|
||||
'[2] b\n[3] c'
|
||||
|
||||
>>> extract_line_range("a\nb\nc", 5, 2) # invalid range
|
||||
''
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Edge cases covered by the test suite
|
||||
|
||||
The 21 tests in `tests/test_line_numbers.py` exercise:
|
||||
|
||||
**`render_with_line_numbers`**
|
||||
- Empty string → `""`
|
||||
- `None` → `""`
|
||||
- Single line
|
||||
- Multi-line
|
||||
- Custom `start_line`
|
||||
- Already-numbered passthrough (no double-prefix)
|
||||
- Already-numbered passthrough with a custom `start_line` (line numbers in source win, not the counter)
|
||||
- Mixed numbered + plain input (counter advances on every line)
|
||||
- Blank lines (still numbered — they are real positions)
|
||||
- Trailing newline semantics (`"a\nb\n".split("\n")` → 3 positions, all numbered)
|
||||
- Input not mutated
|
||||
|
||||
**`extract_line_range`**
|
||||
- Single-line extraction
|
||||
- Inclusive range
|
||||
- Full-document extraction
|
||||
- End beyond length → clipped
|
||||
- Start below 1 → clamped to 1
|
||||
- Start > end → empty
|
||||
- Empty text → empty
|
||||
- Slice containing already-numbered lines (source numbers preserved, counter still advances elsewhere)
|
||||
- Closet-pointer contract: extracting lines 5-7 returns `[5][6][7]`, not `[1][2][3]`
|
||||
- Input not mutated
|
||||
|
||||
All 21 pass on Python 3.9 / pytest 8.4.2 in 0.01s. See `PROOF.md` for verbatim run output.
|
||||
|
||||
---
|
||||
|
||||
## Where to integrate in `mempalace/searcher.py`
|
||||
|
||||
Drop the two functions at module level, alongside the existing helpers (`_tokenize`, `_bm25_scores`, `_hybrid_rank`). They have no dependencies beyond `re` (already imported).
|
||||
|
||||
In the existing `search()` and `search_memories()` flows, when a result row carries explicit `line_start` / `line_end` metadata (e.g. from a closet pointer), pass the drawer text through `extract_line_range` before returning. When a result has no explicit range (BM25 / vector hit on the full drawer body), pass it through `render_with_line_numbers` instead. Both behaviors are additive — existing callers that ignore line numbers get the same string back, just with a `[N] ` prefix per line.
|
||||
|
||||
No new dependencies. No changes to drawer storage. No migration step.
|
||||
|
||||
---
|
||||
|
||||
## Out of scope for 3.3.6 (deferred)
|
||||
|
||||
- **Conversation-boundary detection** (the original `_extract_section` in private tooling expanded line ranges to natural conversation boundaries using timestamp heuristics). That logic is corpus-shape-specific and belongs in its own PR after we agree on the heuristic.
|
||||
- **Day-aggregated drawers** (v4 spec — one drawer per day). 3.3.6 keeps the existing chunk-shaped drawer model. Line numbering works for both, but day-aggregation is a separate architectural change.
|
||||
- **Halls / tunnels.** Unrelated to read-time rendering.
|
||||
- **CLI surface.** No new flags. The behavior is internal to the search pipeline.
|
||||
|
||||
---
|
||||
|
||||
## Backwards compatibility
|
||||
|
||||
- **Drawer storage unchanged.** Existing palaces continue to work without migration.
|
||||
- **Closet pointers unchanged.** The `→date:Lstart-Lend` syntax is unchanged; this PR adds the resolver, not the syntax.
|
||||
- **Public API additive.** Two new module-level functions; nothing renamed or removed.
|
||||
- **No new required arguments** on existing functions. If a caller never passes `line_start` / `line_end`, behavior is identical to today.
|
||||
@@ -671,20 +671,59 @@ def _valid_dimensionality(value: object) -> bool:
|
||||
return isinstance(value, Integral) and not isinstance(value, bool) and int(value) > 0
|
||||
|
||||
|
||||
def _persisted_metadata_fields(obj: object) -> tuple[object, object]:
|
||||
def _persisted_metadata_value(obj: object, name: str) -> object:
|
||||
if isinstance(obj, dict):
|
||||
return obj.get("dimensionality"), obj.get("id_to_label")
|
||||
return getattr(obj, "dimensionality", None), getattr(obj, "id_to_label", None)
|
||||
return obj.get(name)
|
||||
return getattr(obj, name, None)
|
||||
|
||||
|
||||
def _persisted_metadata_fields(obj: object) -> tuple[object, object]:
|
||||
return _persisted_metadata_value(obj, "dimensionality"), _persisted_metadata_value(
|
||||
obj, "id_to_label"
|
||||
)
|
||||
|
||||
|
||||
def _missing_dimensionality_appears_recoverable(
|
||||
persisted: object, id_to_label: dict, seg_dir: str
|
||||
) -> bool:
|
||||
total = _persisted_metadata_value(persisted, "total_elements_added")
|
||||
label_to_id = _persisted_metadata_value(persisted, "label_to_id")
|
||||
data_path = os.path.join(seg_dir, "data_level0.bin")
|
||||
link_path = os.path.join(seg_dir, "link_lists.bin")
|
||||
|
||||
if not isinstance(total, Integral) or isinstance(total, bool):
|
||||
return False
|
||||
if not isinstance(label_to_id, dict):
|
||||
return False
|
||||
try:
|
||||
if not (
|
||||
os.path.isfile(data_path)
|
||||
and os.path.isfile(link_path)
|
||||
and os.path.getsize(data_path) > _HNSW_MISSING_METADATA_DATA_FLOOR
|
||||
):
|
||||
return False
|
||||
except OSError:
|
||||
return False
|
||||
if not _hnsw_payload_appears_sane(seg_dir):
|
||||
return False
|
||||
|
||||
label_count = len(id_to_label)
|
||||
if int(total) != label_count or len(label_to_id) != label_count:
|
||||
return False
|
||||
try:
|
||||
return all(label_to_id.get(label) == item_id for item_id, label in id_to_label.items())
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
|
||||
def quarantine_invalid_hnsw_metadata(palace_path: str) -> list[str]:
|
||||
"""Quarantine segment dirs whose ``index_metadata.pickle`` is unreadable or invalid.
|
||||
|
||||
Chroma's persisted HNSW metadata is untrusted disk state. If a segment has
|
||||
labels but no valid positive dimensionality, current Chroma versions can
|
||||
accept the pickle and crash later in the Rust loader. We rename the entire
|
||||
segment out of the way before ``PersistentClient`` opens so Chroma can
|
||||
rebuild cleanly instead of touching known-bad metadata.
|
||||
labels but invalid or partial metadata, current Chroma versions can accept
|
||||
the pickle and crash later in the Rust loader. We rename the entire segment
|
||||
out of the way before ``PersistentClient`` opens so Chroma can rebuild
|
||||
cleanly instead of touching known-bad metadata.
|
||||
"""
|
||||
try:
|
||||
entries = os.listdir(palace_path)
|
||||
@@ -735,7 +774,22 @@ def quarantine_invalid_hnsw_metadata(palace_path: str) -> list[str]:
|
||||
reason = f"invalid id_to_label type {type(id_to_label).__name__}"
|
||||
else:
|
||||
has_labels = bool(id_to_label)
|
||||
if has_labels and not _valid_dimensionality(dimensionality):
|
||||
if (
|
||||
has_labels
|
||||
and dimensionality is None
|
||||
and not _missing_dimensionality_appears_recoverable(
|
||||
persisted, id_to_label, seg_dir
|
||||
)
|
||||
):
|
||||
reason = (
|
||||
"labels present but dimensionality is missing or invalid "
|
||||
f"({dimensionality!r})"
|
||||
)
|
||||
elif (
|
||||
has_labels
|
||||
and dimensionality is not None
|
||||
and not _valid_dimensionality(dimensionality)
|
||||
):
|
||||
reason = (
|
||||
"labels present but dimensionality is missing or invalid "
|
||||
f"({dimensionality!r})"
|
||||
|
||||
+37
-5
@@ -2,9 +2,10 @@
|
||||
"""
|
||||
MemPalace — Give your AI a memory. No API key required.
|
||||
|
||||
Two ways to ingest:
|
||||
Projects: mempalace mine ~/projects/my_app (code, docs, notes)
|
||||
Conversations: mempalace mine <convo-dir> --mode convos (Claude Code, Claude.ai, ChatGPT, Slack exports)
|
||||
Three ways to ingest:
|
||||
Projects: mempalace mine ~/projects/my_app (code, docs, notes)
|
||||
Conversations: mempalace mine <convo-dir> --mode convos (Claude Code, Claude.ai, ChatGPT, Slack exports)
|
||||
Documents: mempalace mine <docs-dir> --mode extract (PDF, DOCX, PPTX, XLSX, RTF, EPUB — requires mempalace[extract])
|
||||
|
||||
Same palace. Same search. Different ingest strategies.
|
||||
|
||||
@@ -13,6 +14,7 @@ Commands:
|
||||
mempalace split <dir> Split concatenated mega-files into per-session files
|
||||
mempalace mine <dir> Mine project files (default)
|
||||
mempalace mine <dir> --mode convos Mine conversation exports
|
||||
mempalace mine <dir> --mode extract Mine binary office documents (PDF/DOCX/etc.)
|
||||
mempalace search "query" Find anything, exact words
|
||||
mempalace mcp Show MCP setup command
|
||||
mempalace wake-up Show L0 + L1 wake-up context
|
||||
@@ -515,6 +517,17 @@ def cmd_mine(args):
|
||||
dry_run=args.dry_run,
|
||||
extract_mode=args.extract,
|
||||
)
|
||||
elif args.mode == "extract":
|
||||
from .format_miner import mine_formats
|
||||
|
||||
mine_formats(
|
||||
format_dir=args.dir,
|
||||
palace_path=palace_path,
|
||||
wing=args.wing,
|
||||
agent=args.agent,
|
||||
limit=args.limit,
|
||||
dry_run=args.dry_run,
|
||||
)
|
||||
else:
|
||||
from .miner import mine
|
||||
|
||||
@@ -527,6 +540,7 @@ def cmd_mine(args):
|
||||
dry_run=args.dry_run,
|
||||
respect_gitignore=not args.no_gitignore,
|
||||
include_ignored=include_ignored,
|
||||
max_chunks_per_file=getattr(args, "max_chunks_per_file", None),
|
||||
)
|
||||
except MineAlreadyRunning as exc:
|
||||
# A live MCP server or another mine is already writing to this
|
||||
@@ -1260,9 +1274,13 @@ def main():
|
||||
p_mine.add_argument("dir", help="Directory to mine")
|
||||
p_mine.add_argument(
|
||||
"--mode",
|
||||
choices=["projects", "convos"],
|
||||
choices=["projects", "convos", "extract"],
|
||||
default="projects",
|
||||
help="Ingest mode: 'projects' for code/docs (default), 'convos' for chat exports",
|
||||
help=(
|
||||
"Ingest mode: 'projects' for code/docs (default), 'convos' for chat "
|
||||
"exports, 'extract' for office documents (PDF/DOCX/RTF/etc., requires "
|
||||
"mempalace[extract])"
|
||||
),
|
||||
)
|
||||
p_mine.add_argument("--wing", default=None, help="Wing name (default: directory name)")
|
||||
p_mine.add_argument(
|
||||
@@ -1302,6 +1320,20 @@ def main():
|
||||
default="exchange",
|
||||
help="Extraction strategy for convos mode: 'exchange' (default) or 'general' (5 memory types)",
|
||||
)
|
||||
from . import miner as _miner_for_default
|
||||
|
||||
p_mine.add_argument(
|
||||
"--max-chunks-per-file",
|
||||
type=int,
|
||||
default=None,
|
||||
metavar="N",
|
||||
help=(
|
||||
f"Per-file chunk cap; files producing more chunks are skipped with a "
|
||||
f"summary counter. Default {_miner_for_default.MAX_CHUNKS_PER_FILE} "
|
||||
f"(or MEMPALACE_MAX_CHUNKS_PER_FILE). Set 0 to disable. Lower this on "
|
||||
f"Windows if you hit ONNX bad_alloc (#1455)."
|
||||
),
|
||||
)
|
||||
|
||||
# sweep
|
||||
p_sweep = sub.add_parser(
|
||||
|
||||
@@ -287,6 +287,11 @@ class MempalaceConfig:
|
||||
return os.path.abspath(os.path.expanduser(env_val))
|
||||
return self._file_config.get("palace_path", DEFAULT_PALACE_PATH)
|
||||
|
||||
@property
|
||||
def tunnel_file(self):
|
||||
"""Path to the tunnel file, sibling of palace_path."""
|
||||
return os.path.join(os.path.dirname(self.palace_path), "tunnels.json")
|
||||
|
||||
@property
|
||||
def collection_name(self):
|
||||
"""ChromaDB collection name."""
|
||||
|
||||
+138
-43
@@ -15,6 +15,7 @@ import logging
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from collections import defaultdict
|
||||
from typing import Optional
|
||||
|
||||
from .normalize import normalize
|
||||
from .palace import (
|
||||
@@ -24,6 +25,7 @@ from .palace import (
|
||||
file_already_mined,
|
||||
get_collection,
|
||||
mine_lock,
|
||||
mine_palace_lock,
|
||||
prefetch_mined_set,
|
||||
)
|
||||
|
||||
@@ -60,6 +62,8 @@ CONVO_EXTENSIONS = {
|
||||
|
||||
MIN_CHUNK_SIZE = 30
|
||||
CHUNK_SIZE = 800 # chars per drawer — align with miner.py
|
||||
_LINE_GROUP_SIZE = 25 # lines per fallback group when no paragraph breaks
|
||||
_LINE_FALLBACK_MIN_NEWLINES = 20 # trigger line-group fallback above this newline count
|
||||
DRAWER_UPSERT_BATCH_SIZE = 1000
|
||||
MAX_FILE_SIZE = 500 * 1024 * 1024 # 500 MB — skip files larger than this.
|
||||
# Matches miner.py at 500 MB. Long Claude Code sessions, multi-year
|
||||
@@ -163,7 +167,7 @@ def chunk_exchanges(
|
||||
if quote_lines >= 3:
|
||||
return _chunk_by_exchange(lines, chunk_size, min_chunk_size)
|
||||
else:
|
||||
return _chunk_by_paragraph(content, min_chunk_size)
|
||||
return _chunk_by_paragraph(content, chunk_size, min_chunk_size)
|
||||
|
||||
|
||||
def _chunk_by_exchange(lines: list, chunk_size: int, min_chunk_size: int) -> list:
|
||||
@@ -194,49 +198,49 @@ def _chunk_by_exchange(lines: list, chunk_size: int, min_chunk_size: int) -> lis
|
||||
ai_response = " ".join(ai_lines)
|
||||
content = f"{user_turn}\n{ai_response}" if ai_response else user_turn
|
||||
|
||||
# Split into multiple drawers when the exchange exceeds chunk_size
|
||||
if len(content) > chunk_size:
|
||||
# First chunk: user turn + as much response as fits
|
||||
first_part = content[:chunk_size]
|
||||
if len(first_part.strip()) > min_chunk_size:
|
||||
chunks.append({"content": first_part, "chunk_index": len(chunks)})
|
||||
# Remaining response in chunk_size-sized continuation drawers
|
||||
remainder = content[chunk_size:]
|
||||
while remainder:
|
||||
part = remainder[:chunk_size]
|
||||
remainder = remainder[chunk_size:]
|
||||
if len(part.strip()) > min_chunk_size:
|
||||
chunks.append({"content": part, "chunk_index": len(chunks)})
|
||||
elif len(content.strip()) > min_chunk_size:
|
||||
chunks.append(
|
||||
{
|
||||
"content": content,
|
||||
"chunk_index": len(chunks),
|
||||
}
|
||||
)
|
||||
_emit_bounded(chunks, content, chunk_size, min_chunk_size)
|
||||
else:
|
||||
i += 1
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def _chunk_by_paragraph(content: str, min_chunk_size: int) -> list:
|
||||
def _emit_bounded(
|
||||
chunks: list,
|
||||
content: str,
|
||||
chunk_size: int,
|
||||
min_chunk_size: int,
|
||||
) -> None:
|
||||
"""Append ``content`` as one or more drawers, none exceeding ``chunk_size``.
|
||||
|
||||
The ``min_chunk_size`` floor gates the WHOLE call (drops the input if
|
||||
its stripped length is at or below the floor, treated as noise). Once
|
||||
the input passes the floor, every slice is emitted verbatim so a
|
||||
small trailing remainder is preserved instead of silently dropped.
|
||||
The index-based loop avoids the O(N^2) repeated-substring allocation
|
||||
of a ``while content: content = content[chunk_size:]`` shape.
|
||||
"""
|
||||
if len(content.strip()) <= min_chunk_size:
|
||||
return
|
||||
for i in range(0, len(content), chunk_size):
|
||||
chunks.append({"content": content[i : i + chunk_size], "chunk_index": len(chunks)})
|
||||
|
||||
|
||||
def _chunk_by_paragraph(content: str, chunk_size: int, min_chunk_size: int) -> list:
|
||||
"""Fallback: chunk by paragraph breaks."""
|
||||
chunks = []
|
||||
paragraphs = [p.strip() for p in content.split("\n\n") if p.strip()]
|
||||
|
||||
# If no paragraph breaks and long content, chunk by line groups
|
||||
if len(paragraphs) <= 1 and content.count("\n") > 20:
|
||||
if len(paragraphs) <= 1 and content.count("\n") > _LINE_FALLBACK_MIN_NEWLINES:
|
||||
lines = content.split("\n")
|
||||
for i in range(0, len(lines), 25):
|
||||
group = "\n".join(lines[i : i + 25]).strip()
|
||||
if len(group) > min_chunk_size:
|
||||
chunks.append({"content": group, "chunk_index": len(chunks)})
|
||||
for i in range(0, len(lines), _LINE_GROUP_SIZE):
|
||||
group = "\n".join(lines[i : i + _LINE_GROUP_SIZE]).strip()
|
||||
_emit_bounded(chunks, group, chunk_size, min_chunk_size)
|
||||
return chunks
|
||||
|
||||
for para in paragraphs:
|
||||
if len(para) > min_chunk_size:
|
||||
chunks.append({"content": para, "chunk_index": len(chunks)})
|
||||
_emit_bounded(chunks, para, chunk_size, min_chunk_size)
|
||||
|
||||
return chunks
|
||||
|
||||
@@ -448,6 +452,60 @@ def _file_chunks_locked(collection, source_file, chunks, wing, room, agent, extr
|
||||
return drawers_added, room_counts_delta, False
|
||||
|
||||
|
||||
def _is_ai_tool_path(path: Path) -> bool:
|
||||
"""Return True when `path` lives inside a known AI-tool storage dir.
|
||||
|
||||
Detected paths (exact-segment match — substrings like `.gemini-backup`
|
||||
or `.codex-archive` do NOT match):
|
||||
- any segment ``.codex`` (Codex CLI sessions / archives)
|
||||
- any segment ``.gemini`` (Gemini CLI sessions under ~/.gemini/tmp/...)
|
||||
- the consecutive segment pair ``.claude/projects`` (Claude Code).
|
||||
``.claude`` alone is NOT matched — that is the settings/config dir,
|
||||
not a conversation source.
|
||||
|
||||
Used by ``_resolve_wing`` to default the destination wing to
|
||||
``wing_api`` when the user hasn't passed an explicit ``--wing``.
|
||||
"""
|
||||
try:
|
||||
parts = path.resolve().parts
|
||||
except (OSError, RuntimeError):
|
||||
return False
|
||||
|
||||
if ".codex" in parts:
|
||||
return True
|
||||
if ".gemini" in parts:
|
||||
return True
|
||||
for i in range(len(parts) - 1):
|
||||
if parts[i] == ".claude" and parts[i + 1] == "projects":
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _resolve_wing(convo_path: Path, wing: Optional[str]) -> str:
|
||||
"""Determine the destination wing for ``mine_convos``.
|
||||
|
||||
Precedence (first match wins):
|
||||
|
||||
1. Explicit ``wing`` argument from the user — always wins, even on
|
||||
an AI-tool path. Empty string is treated as "no wing".
|
||||
2. AI-tool path detection — defaults to ``wing_api`` so Claude
|
||||
Code / Codex / Gemini conversations group under a single wing
|
||||
dedicated to API-sourced content.
|
||||
3. Basename fallback — sanitized via ``config.normalize_wing_name``
|
||||
(lowercase, spaces/hyphens collapsed to underscores). Shared
|
||||
single source of truth with ``cmd_init``,
|
||||
``room_detector_local``, and ``miner.load_config`` so all
|
||||
wing-slug producers stay in sync (per #1194 consolidation).
|
||||
"""
|
||||
from .config import normalize_wing_name
|
||||
|
||||
if wing:
|
||||
return wing
|
||||
if _is_ai_tool_path(convo_path):
|
||||
return "wing_api"
|
||||
return normalize_wing_name(convo_path.name)
|
||||
|
||||
|
||||
def mine_convos(
|
||||
convo_dir: str,
|
||||
palace_path: str,
|
||||
@@ -463,14 +521,54 @@ def mine_convos(
|
||||
"exchange" — default exchange-pair chunking (Q+A = one unit)
|
||||
"general" — general extractor: decisions, preferences, milestones, problems, emotions
|
||||
|
||||
The real work is in :func:`_mine_convos_impl`; this wrapper holds the
|
||||
per-palace flock around it so two concurrent ``mempalace mine --mode
|
||||
convos`` invocations against the same palace can't pile up. This
|
||||
mirrors the pattern in :func:`mempalace.miner.mine`. The lock is
|
||||
non-blocking: ``MineAlreadyRunning`` propagates to the CLI (which
|
||||
renders a holder-aware message and exits non-zero) or to in-process
|
||||
callers that expect to coexist with another writer.
|
||||
|
||||
Dry-run skips the lock — it never writes to the palace and so cannot
|
||||
corrupt anything, and skipping the lock lets dry-run probes coexist
|
||||
with a live mine.
|
||||
|
||||
Chunking parameters (chunk_size, min_chunk_size) are read from
|
||||
MempalaceConfig so `config.json` governs both this path and the
|
||||
project-file miner in `miner.py`. `min_chunk_size` preserves
|
||||
convo_miner's lower default (30 — more permissive than the 50-char
|
||||
project default, so short conversation exchanges are not dropped)
|
||||
when not explicitly set in config.json, so a user who never touches
|
||||
chunking keeps the existing behavior.
|
||||
MempalaceConfig inside :func:`_mine_convos_impl` so `config.json`
|
||||
governs both this path and the project-file miner in `miner.py`.
|
||||
"""
|
||||
if dry_run:
|
||||
return _mine_convos_impl(
|
||||
convo_dir,
|
||||
palace_path,
|
||||
wing=wing,
|
||||
agent=agent,
|
||||
limit=limit,
|
||||
dry_run=dry_run,
|
||||
extract_mode=extract_mode,
|
||||
)
|
||||
|
||||
with mine_palace_lock(palace_path):
|
||||
return _mine_convos_impl(
|
||||
convo_dir,
|
||||
palace_path,
|
||||
wing=wing,
|
||||
agent=agent,
|
||||
limit=limit,
|
||||
dry_run=dry_run,
|
||||
extract_mode=extract_mode,
|
||||
)
|
||||
|
||||
|
||||
def _mine_convos_impl(
|
||||
convo_dir: str,
|
||||
palace_path: str,
|
||||
wing: str = None,
|
||||
agent: str = "mempalace",
|
||||
limit: int = 0,
|
||||
dry_run: bool = False,
|
||||
extract_mode: str = "exchange",
|
||||
):
|
||||
from .config import MempalaceConfig
|
||||
|
||||
palace_config = MempalaceConfig()
|
||||
@@ -480,17 +578,14 @@ def mine_convos(
|
||||
# validated value or None — None keeps convo's lower 30-char floor
|
||||
# (more permissive than the 50-char project default, so short
|
||||
# exchanges aren't dropped). Using the validated accessor (not raw
|
||||
# _file_config) means a
|
||||
# garbage/negative/bool config value can't TypeError the length gate
|
||||
# below or ValueError out of chunk_exchanges and abort convo ingest.
|
||||
# _file_config) means a garbage/negative/bool config value can't
|
||||
# TypeError the length gate below or ValueError out of
|
||||
# chunk_exchanges and abort convo ingest.
|
||||
explicit_min = palace_config.min_chunk_size_explicit
|
||||
cfg_min_chunk_size = explicit_min if explicit_min is not None else MIN_CHUNK_SIZE
|
||||
|
||||
convo_path = Path(convo_dir).expanduser().resolve()
|
||||
if not wing:
|
||||
from .config import normalize_wing_name
|
||||
|
||||
wing = normalize_wing_name(convo_path.name)
|
||||
wing = _resolve_wing(convo_path, wing)
|
||||
|
||||
files = scan_convos(convo_dir)
|
||||
if limit > 0:
|
||||
@@ -547,7 +642,7 @@ def mine_convos(
|
||||
if extract_mode == "general":
|
||||
from .general_extractor import extract_memories
|
||||
|
||||
chunks = extract_memories(content)
|
||||
chunks = extract_memories(content, chunk_size=cfg_chunk_size)
|
||||
# Each chunk already has memory_type; use it as the room name
|
||||
else:
|
||||
chunks = chunk_exchanges(
|
||||
|
||||
+128
-17
@@ -18,11 +18,13 @@ Usage:
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from .config import MempalaceConfig
|
||||
from .miner import _extract_entities_for_metadata
|
||||
from .palace import (
|
||||
build_closet_lines,
|
||||
@@ -33,6 +35,8 @@ from .palace import (
|
||||
upsert_closet_lines,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DIARY_ENTRY_RE = re.compile(r"^## .+", re.MULTILINE)
|
||||
|
||||
|
||||
@@ -61,12 +65,35 @@ def _split_entries(text):
|
||||
|
||||
|
||||
def _diary_drawer_id(wing: str, date_str: str) -> str:
|
||||
"""Stable, wing-scoped drawer ID. Two diaries (e.g. 'work' vs 'personal')
|
||||
sharing the same date never collide."""
|
||||
"""Stable, wing-scoped legacy drawer ID (file-level).
|
||||
|
||||
Retained for backwards-compatible cleanup of palaces that ingested
|
||||
diaries before #1539 — those palaces hold one ``drawer_diary_{...}``
|
||||
per file. New drawers use ``_diary_drawer_id_entry`` so each ``##``
|
||||
entry becomes its own drawer (with per-entry character chunking
|
||||
when an entry exceeds ``chunk_size``).
|
||||
"""
|
||||
suffix = hashlib.sha256(f"{wing}|{date_str}".encode()).hexdigest()[:24]
|
||||
return f"drawer_diary_{suffix}"
|
||||
|
||||
|
||||
def _diary_drawer_id_entry(wing: str, date_str: str, entry_idx: int, entry_chunk_idx: int) -> str:
|
||||
"""Per-entry, per-chunk drawer ID introduced in #1539.
|
||||
|
||||
The ``v2_`` prefix distinguishes new IDs from the legacy file-level
|
||||
scheme (one drawer per file). Legacy drawers from pre-#1539 palaces
|
||||
are auto-purged on any ``ingest_diaries`` call that triggers a full
|
||||
rebuild (``force=True`` or detected content change). The per-source
|
||||
``delete(where=...)`` step on full rebuild collects both legacy and
|
||||
stale v2 drawers via the ``source_file`` metadata key, so the
|
||||
schema migration runs as a side effect of normal use.
|
||||
"""
|
||||
suffix = hashlib.sha256(
|
||||
f"{wing}|{date_str}|{entry_idx}|{entry_chunk_idx}".encode()
|
||||
).hexdigest()[:24]
|
||||
return f"drawer_diary_v2_{suffix}"
|
||||
|
||||
|
||||
def _diary_closet_id_base(wing: str, date_str: str) -> str:
|
||||
suffix = hashlib.sha256(f"{wing}|{date_str}".encode()).hexdigest()[:24]
|
||||
return f"closet_diary_{suffix}"
|
||||
@@ -106,6 +133,7 @@ def ingest_diaries(
|
||||
|
||||
drawers_col = get_collection(palace_path)
|
||||
closets_col = get_closets_collection(palace_path)
|
||||
chunk_size = MempalaceConfig().chunk_size
|
||||
|
||||
days_updated = 0
|
||||
closets_created = 0
|
||||
@@ -144,14 +172,17 @@ def ingest_diaries(
|
||||
content_changed = prev_hash is not None and curr_hash != prev_hash
|
||||
|
||||
now_iso = datetime.now(timezone.utc).isoformat()
|
||||
drawer_id = _diary_drawer_id(wing, date_str)
|
||||
entities = _extract_entities_for_metadata(text)
|
||||
source_file = str(diary_path)
|
||||
|
||||
# Serialize per source — two terminals running ingest at once must
|
||||
# not interleave the upsert + closet-rebuild.
|
||||
with mine_lock(source_file):
|
||||
drawer_meta = {
|
||||
entries = _split_entries(text)
|
||||
prev_entry_count = state.get(state_key, {}).get("entry_count", 0)
|
||||
full_rebuild = force or content_changed
|
||||
|
||||
base_meta = {
|
||||
"date": date_str,
|
||||
"wing": wing,
|
||||
"room": "daily",
|
||||
@@ -160,24 +191,104 @@ def ingest_diaries(
|
||||
"filed_at": now_iso,
|
||||
}
|
||||
if entities:
|
||||
drawer_meta["entities"] = entities
|
||||
drawers_col.upsert(
|
||||
documents=[text],
|
||||
ids=[drawer_id],
|
||||
metadatas=[drawer_meta],
|
||||
)
|
||||
base_meta["entities"] = entities
|
||||
|
||||
# On full rebuild, purge ALL prior drawers for this source_file
|
||||
# before writing fresh entries. This covers three cases in one
|
||||
# step: (a) legacy pre-#1539 file-level drawers under the old
|
||||
# ``drawer_diary_`` prefix, (b) v2 drawers from a prior pass
|
||||
# where the file had MORE entries than the current run (entry
|
||||
# deletion would otherwise leave trailing orphan drawers), and
|
||||
# (c) in-place edits that shift content across entry boundaries.
|
||||
if full_rebuild:
|
||||
try:
|
||||
drawers_col.delete(where={"source_file": source_file})
|
||||
except Exception as exc:
|
||||
# ChromaDB ``delete(where=...)`` against an empty collection
|
||||
# returns silently rather than raising, so this catch only
|
||||
# fires on real backend errors (locked DB, schema mismatch,
|
||||
# transient I/O). Log at debug to preserve diagnostics
|
||||
# without aborting the rebuild: the upsert below is the
|
||||
# load-bearing write, and if it also fails the state file
|
||||
# is left unchanged so the next pass retries cleanly.
|
||||
logger.debug(
|
||||
"legacy purge skipped for %s: %s",
|
||||
source_file,
|
||||
exc,
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
# Per-entry drawers, with character chunking inside any entry
|
||||
# whose serialized text exceeds chunk_size. ``chunk_index`` is
|
||||
# a global counter across the file so the searcher's
|
||||
# ``_expand_with_neighbors`` (which queries by source_file +
|
||||
# chunk_index) can stitch sibling chunks back together
|
||||
# regardless of entry boundary. ``entry_index`` and
|
||||
# ``entry_chunk_index`` are preserved for entry-grouping
|
||||
# consumers.
|
||||
#
|
||||
# Accumulate all drawers for the file into one batched upsert
|
||||
# so the embedding pass either commits every chunk or none.
|
||||
# A mid-loop embedding failure would otherwise leave a
|
||||
# half-written day's drawers behind, which the next
|
||||
# incremental pass would skip (state file already updated)
|
||||
# and the searcher would silently return partial results.
|
||||
global_chunk_index = 0
|
||||
batch_ids: list[str] = []
|
||||
batch_docs: list[str] = []
|
||||
batch_metas: list[dict] = []
|
||||
for entry_idx, (header, body) in enumerate(entries):
|
||||
entry_text = f"{header}\n{body}" if body else header
|
||||
if len(entry_text) <= chunk_size:
|
||||
batch_ids.append(_diary_drawer_id_entry(wing, date_str, entry_idx, 0))
|
||||
batch_docs.append(entry_text)
|
||||
batch_metas.append(
|
||||
{
|
||||
**base_meta,
|
||||
"chunk_index": global_chunk_index,
|
||||
"entry_index": entry_idx,
|
||||
"entry_chunk_index": 0,
|
||||
"entry_header_preview": header[:120],
|
||||
}
|
||||
)
|
||||
global_chunk_index += 1
|
||||
else:
|
||||
for entry_chunk_idx, start in enumerate(range(0, len(entry_text), chunk_size)):
|
||||
batch_ids.append(
|
||||
_diary_drawer_id_entry(wing, date_str, entry_idx, entry_chunk_idx)
|
||||
)
|
||||
batch_docs.append(entry_text[start : start + chunk_size])
|
||||
batch_metas.append(
|
||||
{
|
||||
**base_meta,
|
||||
"chunk_index": global_chunk_index,
|
||||
"entry_index": entry_idx,
|
||||
"entry_chunk_index": entry_chunk_idx,
|
||||
"entry_header_preview": header[:120],
|
||||
}
|
||||
)
|
||||
global_chunk_index += 1
|
||||
|
||||
if batch_ids:
|
||||
drawers_col.upsert(
|
||||
ids=batch_ids,
|
||||
documents=batch_docs,
|
||||
metadatas=batch_metas,
|
||||
)
|
||||
|
||||
entries = _split_entries(text)
|
||||
prev_entry_count = state.get(state_key, {}).get("entry_count", 0)
|
||||
full_rebuild = force or content_changed
|
||||
new_entries = entries if full_rebuild else entries[prev_entry_count:]
|
||||
|
||||
if new_entries:
|
||||
all_lines = []
|
||||
for header, body in new_entries:
|
||||
entry_text = f"{header}\n{body}"
|
||||
for offset, (header, body) in enumerate(new_entries):
|
||||
entry_idx = offset if full_rebuild else prev_entry_count + offset
|
||||
entry_text = f"{header}\n{body}" if body else header
|
||||
# Closet references the canonical (entry_chunk_idx=0)
|
||||
# drawer for the entry. Searcher._expand_with_neighbors
|
||||
# stitches sibling chunks back via the
|
||||
# (source_file, chunk_index) pair.
|
||||
entry_drawer_id = _diary_drawer_id_entry(wing, date_str, entry_idx, 0)
|
||||
entry_lines = build_closet_lines(
|
||||
source_file, [drawer_id], entry_text, wing, "daily"
|
||||
source_file, [entry_drawer_id], entry_text, wing, "daily"
|
||||
)
|
||||
all_lines.extend(entry_lines)
|
||||
|
||||
|
||||
@@ -0,0 +1,950 @@
|
||||
"""format_miner.py — proposed for mempalace 3.3.6.
|
||||
|
||||
A third miner alongside ``miner.py`` (project files) and ``convo_miner.py``
|
||||
(chat exports). This one handles **binary office-format documents**:
|
||||
PDF, DOCX, PPTX, XLSX, RTF, EPUB.
|
||||
|
||||
Architecture matches the existing miner-per-content-type pattern:
|
||||
|
||||
mempalace mine <dir> → miner.py
|
||||
mempalace mine <dir> --mode convos → convo_miner.py
|
||||
mempalace mine <dir> --mode extract → format_miner.py (this file)
|
||||
|
||||
**Read-time conversion**, never modifies source files on disk. The user's
|
||||
``~/research.pdf`` stays exactly as it was after a mine — the bytes are
|
||||
read into memory, handed to Microsoft's MarkItDown library for conversion
|
||||
to Markdown, and the resulting text flows into the normal chunker /
|
||||
drawer-store pipeline. Conversion artifacts never touch disk.
|
||||
|
||||
MarkItDown is an **optional** runtime dependency (declared as an extra).
|
||||
If the user runs ``mempalace mine --mode extract`` without it installed,
|
||||
they get a clear ``pip install markitdown`` instruction, not a crash.
|
||||
|
||||
**Per-format transformer routing** (verified live on a local mixed-format
|
||||
test directory, 2026-05-19): MarkItDown 0.1.5 does NOT convert .rtf —
|
||||
it returns the raw RTF control-code source unchanged. So .rtf is routed
|
||||
to the purpose-built ``striprtf`` library; all other formats stay on
|
||||
MarkItDown.
|
||||
|
||||
.pdf, .docx, .pptx, .xlsx, .epub → MarkItDown
|
||||
.rtf → striprtf
|
||||
|
||||
Both libraries are optional runtime dependencies. If a user tries to
|
||||
extract a format whose transformer isn't installed, they get a clear
|
||||
install message (SKIP_NO_MARKITDOWN or SKIP_NO_STRIPRTF), not a crash.
|
||||
|
||||
**13 fringe cases handled** (spec finalized 2026-05-19):
|
||||
|
||||
1. MarkItDown not installed → SKIP_NO_MARKITDOWN (clear install msg)
|
||||
2. File too large (> max) → SKIP_TOO_LARGE
|
||||
3. iCloud cloud-only file → SKIP_CLOUD_ONLY
|
||||
4. Encrypted PDF → SKIP_ENCRYPTED
|
||||
5. Empty file → SKIP_EMPTY
|
||||
6. Permission denied → SKIP_PERMISSION
|
||||
7. Broken symlink → SKIP_BROKEN_SYMLINK
|
||||
8. Dirty encoding → recovered via decode_robust
|
||||
9. Windows path semantics → pathlib throughout
|
||||
10. MarkItDown internal crash → SKIP_EXTRACTION_ERROR
|
||||
11. Network / sync timeout → SKIP_NETWORK_TIMEOUT
|
||||
12. Unrecognized extension → SKIP_UNRECOGNIZED
|
||||
13. striprtf not installed → SKIP_NO_STRIPRTF (added 2026-05-19
|
||||
after live test
|
||||
on local RTF files)
|
||||
|
||||
Deferred (out of scope): custom PDF parsers for specific document types,
|
||||
OCR on scanned PDFs, DRM-locked files, pathological corrupt files.
|
||||
These get reported and skipped.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
import hashlib
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
|
||||
from .palace import (
|
||||
NORMALIZE_VERSION,
|
||||
SKIP_DIRS,
|
||||
file_already_mined,
|
||||
get_collection,
|
||||
mine_lock,
|
||||
)
|
||||
|
||||
# Module-level imports from .miner so tests can patch them via
|
||||
# mempalace.format_miner.<name>. Lazy imports inside functions would not
|
||||
# expose these as attributes of this module, breaking the test seams.
|
||||
from .config import MempalaceConfig, normalize_wing_name
|
||||
from .miner import (
|
||||
_compute_topic_tunnels_for_wing,
|
||||
chunk_text,
|
||||
detect_room,
|
||||
load_config,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"SUPPORTED_FORMATS",
|
||||
"DEFAULT_MAX_FILE_SIZE",
|
||||
"ExtractionStatus",
|
||||
"decode_robust",
|
||||
"is_icloud_dataless",
|
||||
"extract_text",
|
||||
"scan_formats",
|
||||
"mine_formats",
|
||||
]
|
||||
|
||||
|
||||
# Same batch size as miner.py / convo_miner.py — bounds memory + Chroma payload.
|
||||
DRAWER_UPSERT_BATCH_SIZE = 1000
|
||||
|
||||
# Minimum chunk size (chars) — drawers below this are dropped as not useful.
|
||||
MIN_CHUNK_SIZE = 50
|
||||
|
||||
|
||||
logger = logging.getLogger("mempalace_format_miner")
|
||||
|
||||
|
||||
# Extensions MarkItDown can convert. Lowercase, leading dot. Case-insensitive
|
||||
# match performed against ``Path.suffix.lower()``.
|
||||
SUPPORTED_FORMATS = frozenset(
|
||||
{
|
||||
".pdf",
|
||||
".docx",
|
||||
".pptx",
|
||||
".xlsx",
|
||||
".rtf",
|
||||
".epub",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# Same default cap as miner.py / convo_miner.py — guards against pathological
|
||||
# binary files and runaway memory. The caller can override via
|
||||
# ``extract_text(..., max_file_size=...)`` for legitimately large documents.
|
||||
DEFAULT_MAX_FILE_SIZE = 500 * 1024 * 1024 # 500 MB
|
||||
|
||||
|
||||
# Filename patterns that are not user content even if their extension matches.
|
||||
_SKIP_FILENAMES = {
|
||||
".DS_Store",
|
||||
"Thumbs.db",
|
||||
"desktop.ini",
|
||||
}
|
||||
|
||||
|
||||
# Message patterns that identify an encrypted / password-protected PDF
|
||||
# regardless of which underlying library raised the exception.
|
||||
_ENCRYPTED_PATTERNS = re.compile(
|
||||
r"(encrypt|decrypt|password|protected)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
class ExtractionStatus(enum.Enum):
|
||||
"""Outcome of an ``extract_text`` call.
|
||||
|
||||
Test-friendly enum (each case has its own name) so callers can assert
|
||||
exactly which path was taken. ``OK`` means text came back; everything
|
||||
else is a skip variant.
|
||||
"""
|
||||
|
||||
OK = "ok"
|
||||
SKIP_TOO_LARGE = "skip:too_large"
|
||||
SKIP_CLOUD_ONLY = "skip:cloud_only"
|
||||
SKIP_EMPTY = "skip:empty"
|
||||
SKIP_NO_MARKITDOWN = "skip:no_markitdown"
|
||||
SKIP_NO_STRIPRTF = "skip:no_striprtf"
|
||||
SKIP_ENCRYPTED = "skip:encrypted"
|
||||
SKIP_PERMISSION = "skip:permission"
|
||||
SKIP_BROKEN_SYMLINK = "skip:broken_symlink"
|
||||
SKIP_UNRECOGNIZED = "skip:unrecognized"
|
||||
SKIP_EXTRACTION_ERROR = "skip:extraction_error"
|
||||
SKIP_MISSING_FORMAT_DEPS = "skip:missing_format_deps"
|
||||
SKIP_NETWORK_TIMEOUT = "skip:network_timeout"
|
||||
SKIP_UNREADABLE = "skip:unreadable"
|
||||
|
||||
|
||||
# Skip variants that represent TRANSIENT environmental issues — the optional
|
||||
# transformer dep isn't installed yet, or the network blipped. Sentinel
|
||||
# writes for these would permanently mark the file as "already mined" and
|
||||
# defeat re-mining after the missing piece is installed / the network is
|
||||
# back. The orchestrator (``mine_formats``) checks this set before calling
|
||||
# ``_register_file``. Per PR #1555 review (Copilot #14).
|
||||
_TRANSIENT_MISSING_DEP_STATUSES = frozenset(
|
||||
{
|
||||
ExtractionStatus.SKIP_NO_MARKITDOWN,
|
||||
ExtractionStatus.SKIP_NO_STRIPRTF,
|
||||
ExtractionStatus.SKIP_MISSING_FORMAT_DEPS,
|
||||
ExtractionStatus.SKIP_NETWORK_TIMEOUT,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Encoding fallback — same pattern as the terminal-session normalizer
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def decode_robust(raw: bytes) -> str:
|
||||
"""Decode bytes to text without raising on dirty encodings.
|
||||
|
||||
Strategy: UTF-8 first (the clean case). On failure, try CP1252 (handles
|
||||
legacy smart-quote bytes 0x91-0x9F that surface in older Office docs).
|
||||
Final fallback is UTF-8 with ``errors='replace'`` so no byte is ever
|
||||
lost — only made visible as the replacement char.
|
||||
"""
|
||||
if not raw:
|
||||
return ""
|
||||
try:
|
||||
return raw.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
try:
|
||||
return raw.decode("cp1252")
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
return raw.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# iCloud cloud-only file detection
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def is_icloud_dataless(path: Path) -> bool:
|
||||
"""True if ``path`` is an iCloud cloud-only placeholder (not local).
|
||||
|
||||
Two indicators:
|
||||
1. The literal ``.icloud`` suffix iCloud uses for offloaded files.
|
||||
2. The macOS UF_COMPRESSED / dataless flag on the inode (``st_flags``).
|
||||
|
||||
Either signal means MarkItDown would block on file I/O waiting for
|
||||
iCloud to materialize the bytes, which can hang for minutes. We skip
|
||||
these and report.
|
||||
"""
|
||||
if path.suffix.lower() == ".icloud":
|
||||
return True
|
||||
# macOS-only: stat() exposes st_flags which carries the dataless flag.
|
||||
# On other platforms this attribute is absent or always 0.
|
||||
try:
|
||||
flags = getattr(path.lstat(), "st_flags", 0)
|
||||
except OSError:
|
||||
return False
|
||||
# SF_DATALESS = 0x40000000 on macOS. Avoid hardcoding magic numbers
|
||||
# cross-platform; check the bit if it exists.
|
||||
DATALESS_FLAG = 0x40000000
|
||||
return bool(flags & DATALESS_FLAG)
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Conversion via MarkItDown — isolated so tests can patch this single seam
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _extract_via_markitdown(path: Path) -> Optional[str]:
|
||||
"""Run MarkItDown on ``path`` and return the resulting Markdown text.
|
||||
|
||||
Used for .pdf, .docx, .pptx, .xlsx, .epub. NOT used for .rtf — MarkItDown
|
||||
0.1.5 doesn't convert RTF (returns raw control-code source unchanged),
|
||||
so .rtf is routed to ``_extract_via_striprtf`` instead.
|
||||
|
||||
Raises ``ImportError`` if the markitdown package is not installed
|
||||
(caller's responsibility to translate this into ``SKIP_NO_MARKITDOWN``).
|
||||
|
||||
Raises any other exception MarkItDown raises — the caller classifies
|
||||
them via message pattern (encrypted vs generic crash vs timeout).
|
||||
|
||||
Returns ``None`` if MarkItDown returns an empty result for the file.
|
||||
"""
|
||||
try:
|
||||
from markitdown import MarkItDown # type: ignore[import-not-found]
|
||||
except ImportError:
|
||||
raise
|
||||
|
||||
converter = MarkItDown()
|
||||
result = converter.convert(str(path))
|
||||
text = getattr(result, "text_content", None) or getattr(result, "markdown", None)
|
||||
if text is None:
|
||||
return None
|
||||
if not isinstance(text, str):
|
||||
return None
|
||||
return text
|
||||
|
||||
|
||||
def _extract_via_striprtf(path: Path) -> Optional[str]:
|
||||
"""Run striprtf on ``path`` and return plain text.
|
||||
|
||||
Used exclusively for .rtf (see ``_extract_via_markitdown`` docstring for
|
||||
why). striprtf is pure-Python, MIT-licensed, ~150 lines, cross-platform
|
||||
(works on Python 3.6+).
|
||||
|
||||
Raises ``ImportError`` if the striprtf package is not installed
|
||||
(caller's responsibility to translate this into ``SKIP_NO_STRIPRTF``).
|
||||
|
||||
Returns ``None`` if striprtf strips the file to empty text.
|
||||
"""
|
||||
try:
|
||||
from striprtf.striprtf import rtf_to_text # type: ignore[import-not-found]
|
||||
except ImportError:
|
||||
raise
|
||||
|
||||
raw = path.read_bytes()
|
||||
source = decode_robust(raw)
|
||||
text = rtf_to_text(source)
|
||||
if not isinstance(text, str):
|
||||
return None
|
||||
if text == "":
|
||||
return None
|
||||
return text
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# extract_text — the dispatcher with all 12 fringe cases handled
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def extract_text(
|
||||
path: Union[Path, str],
|
||||
max_file_size: int = DEFAULT_MAX_FILE_SIZE,
|
||||
) -> tuple[Optional[str], ExtractionStatus]:
|
||||
"""Convert ``path`` to plain text via MarkItDown, with comprehensive fringe-case handling.
|
||||
|
||||
Returns ``(text, ExtractionStatus)`` — text is ``None`` for every skip
|
||||
case, a non-empty string for ``OK``.
|
||||
|
||||
Pure function (no I/O outside the file at ``path``). Source file is
|
||||
never modified.
|
||||
"""
|
||||
# Accept str or Path; pathlib handles Windows separators correctly.
|
||||
# ``expanduser()`` resolves ``~/foo.pdf`` style paths so CLI inputs work
|
||||
# without forcing the caller to pre-resolve. Per PR #1555 review (Copilot).
|
||||
p = Path(path).expanduser()
|
||||
|
||||
# Fringe Case 7 — broken symlink.
|
||||
# ``is_symlink()`` true + ``exists()`` false ⇒ link target is missing.
|
||||
if p.is_symlink() and not p.exists():
|
||||
logger.info("skip:broken_symlink %s", p)
|
||||
return None, ExtractionStatus.SKIP_BROKEN_SYMLINK
|
||||
|
||||
# Fringe Case 3 — iCloud cloud-only file. Detect BEFORE stat() so we
|
||||
# don't accidentally trigger an iCloud materialization fetch.
|
||||
if is_icloud_dataless(p):
|
||||
logger.info("skip:cloud_only %s", p)
|
||||
return None, ExtractionStatus.SKIP_CLOUD_ONLY
|
||||
|
||||
# General readability check. Anything unreadable here (file gone,
|
||||
# permission denied at stat-time, etc.) collapses to SKIP_UNREADABLE
|
||||
# or SKIP_PERMISSION depending on the error class.
|
||||
try:
|
||||
stat = p.stat()
|
||||
except PermissionError:
|
||||
logger.info("skip:permission (stat) %s", p)
|
||||
return None, ExtractionStatus.SKIP_PERMISSION
|
||||
except FileNotFoundError:
|
||||
# Distinguish "broken symlink" (target gone) from "file disappeared
|
||||
# between scan and extract" (regular file, no longer exists). The
|
||||
# is_symlink() check at the top of this function catches the symlink
|
||||
# case BEFORE stat(), so by the time we land here the symlink path
|
||||
# is rare — but a race between scan_formats and extract_text on a
|
||||
# plain file is the common cause. Per PR #1555 review (Copilot #8).
|
||||
if p.is_symlink():
|
||||
logger.info("skip:broken_symlink (stat) %s", p)
|
||||
return None, ExtractionStatus.SKIP_BROKEN_SYMLINK
|
||||
logger.info("skip:unreadable (file gone after scan) %s", p)
|
||||
return None, ExtractionStatus.SKIP_UNREADABLE
|
||||
except OSError as exc:
|
||||
logger.info("skip:unreadable %s — %s", p, exc)
|
||||
return None, ExtractionStatus.SKIP_UNREADABLE
|
||||
|
||||
# Fringe Case 5 — empty file. Skip silently.
|
||||
if stat.st_size == 0:
|
||||
logger.debug("skip:empty %s", p)
|
||||
return None, ExtractionStatus.SKIP_EMPTY
|
||||
|
||||
# Fringe Case 2 — file exceeds the caller's cap. The default is 500 MB
|
||||
# (same as the existing miners). Callers with legitimate large docs
|
||||
# raise the cap explicitly.
|
||||
if stat.st_size > max_file_size:
|
||||
logger.info("skip:too_large %s (%d bytes > %d)", p, stat.st_size, max_file_size)
|
||||
return None, ExtractionStatus.SKIP_TOO_LARGE
|
||||
|
||||
# Fringe Case 12 — unrecognized extension. Cheap to check, do it last
|
||||
# so we still report size / cloud / symlink issues for the obvious cases.
|
||||
if p.suffix.lower() not in SUPPORTED_FORMATS:
|
||||
logger.debug("skip:unrecognized %s (suffix=%s)", p, p.suffix)
|
||||
return None, ExtractionStatus.SKIP_UNRECOGNIZED
|
||||
|
||||
# Per-format transformer routing. .rtf goes to striprtf (MarkItDown
|
||||
# 0.1.5 doesn't convert RTF — returns raw control-code source
|
||||
# unchanged, verified live on a local RTF set 2026-05-19).
|
||||
is_rtf = p.suffix.lower() == ".rtf"
|
||||
try:
|
||||
if is_rtf:
|
||||
text = _extract_via_striprtf(p)
|
||||
else:
|
||||
text = _extract_via_markitdown(p)
|
||||
except ImportError:
|
||||
# Fringe Case 1 / 13 — transformer not installed in this env.
|
||||
if is_rtf:
|
||||
logger.warning(
|
||||
"skip:no_striprtf %s — install with: pip install striprtf",
|
||||
p,
|
||||
)
|
||||
return None, ExtractionStatus.SKIP_NO_STRIPRTF
|
||||
logger.warning(
|
||||
"skip:no_markitdown %s — install with: pip install markitdown",
|
||||
p,
|
||||
)
|
||||
return None, ExtractionStatus.SKIP_NO_MARKITDOWN
|
||||
except TimeoutError:
|
||||
# Fringe Case 11 — network or sync-drive timeout.
|
||||
logger.info("skip:network_timeout %s", p)
|
||||
return None, ExtractionStatus.SKIP_NETWORK_TIMEOUT
|
||||
except PermissionError:
|
||||
# Fringe Case 6 — permission denied during file read.
|
||||
logger.info("skip:permission %s", p)
|
||||
return None, ExtractionStatus.SKIP_PERMISSION
|
||||
except Exception as exc:
|
||||
# Fringe Case 14 (PR #1555 review, Igor): MarkItDown raises
|
||||
# ``MissingDependencyException`` when a per-format sub-extra
|
||||
# (e.g. ``markitdown[pdf]``) isn't installed. Surface this as a
|
||||
# distinct status so users see the actionable signal instead of
|
||||
# the generic SKIP_EXTRACTION_ERROR. Match by type name (not
|
||||
# isinstance) so the catch fires without requiring markitdown to
|
||||
# be import-resolvable at module load — keeps the static-import
|
||||
# surface unchanged.
|
||||
if type(exc).__name__ == "MissingDependencyException":
|
||||
logger.warning(
|
||||
"skip:missing_format_deps %s — %s: %s",
|
||||
p,
|
||||
type(exc).__name__,
|
||||
str(exc)[:200],
|
||||
)
|
||||
return None, ExtractionStatus.SKIP_MISSING_FORMAT_DEPS
|
||||
# Fringe Case 4 vs Case 10: encrypted vs generic crash, by message.
|
||||
msg = str(exc)
|
||||
if _ENCRYPTED_PATTERNS.search(msg):
|
||||
logger.info("skip:encrypted %s — %s", p, msg[:120])
|
||||
return None, ExtractionStatus.SKIP_ENCRYPTED
|
||||
logger.warning("skip:extraction_error %s — %s: %s", p, type(exc).__name__, msg[:200])
|
||||
return None, ExtractionStatus.SKIP_EXTRACTION_ERROR
|
||||
|
||||
# Either transformer can legitimately return None / empty (malformed
|
||||
# docs that parse but extract nothing). Treat both as extraction error
|
||||
# so the caller knows to skip rather than file an empty drawer.
|
||||
if not text:
|
||||
transformer = "striprtf" if is_rtf else "markitdown"
|
||||
logger.info("skip:extraction_error %s — %s returned None/empty", p, transformer)
|
||||
return None, ExtractionStatus.SKIP_EXTRACTION_ERROR
|
||||
|
||||
return text, ExtractionStatus.OK
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Directory walker
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def scan_formats(directory: Union[Path, str]) -> list[Path]:
|
||||
"""Walk ``directory`` recursively and return supported files, sorted.
|
||||
|
||||
Skips:
|
||||
- Hidden / build directories listed in ``palace.SKIP_DIRS``
|
||||
- Filenames listed in ``_SKIP_FILENAMES`` (.DS_Store etc.)
|
||||
- Symlinks (prevents circular links / processing the same file via
|
||||
multiple paths; mirrors ``miner.py`` and ``convo_miner.py``)
|
||||
- Files whose suffix isn't in ``SUPPORTED_FORMATS``
|
||||
|
||||
Returns a list of ``Path`` objects. The order is deterministic
|
||||
(sorted by path) so a re-mine processes files in the same order each
|
||||
time — useful for reproducing bug reports.
|
||||
"""
|
||||
# ``expanduser().resolve()`` normalizes ``~/docs`` and relative paths so
|
||||
# CLI inputs like ``mempalace mine --mode extract ~/docs`` work without
|
||||
# the caller pre-resolving. Per PR #1555 review (Copilot #9).
|
||||
root = Path(directory).expanduser().resolve()
|
||||
if not root.exists():
|
||||
return []
|
||||
|
||||
found: list[Path] = []
|
||||
for dirpath, dirnames, filenames in os.walk(root):
|
||||
# Mutate dirnames in place to prune the walk. Uses the shared
|
||||
# palace.SKIP_DIRS constant so this stays in sync with the other
|
||||
# miners' skip set.
|
||||
dirnames[:] = [d for d in dirnames if d not in SKIP_DIRS]
|
||||
for name in filenames:
|
||||
if name in _SKIP_FILENAMES:
|
||||
continue
|
||||
p = Path(dirpath) / name
|
||||
# Skip symlinks — prevents following links to /dev/urandom,
|
||||
# circular links, or processing the same file twice via
|
||||
# different paths. Mirrors miner.py:1068.
|
||||
if p.is_symlink():
|
||||
continue
|
||||
if p.suffix.lower() not in SUPPORTED_FORMATS:
|
||||
continue
|
||||
found.append(p)
|
||||
|
||||
found.sort()
|
||||
return found
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# mine_formats — orchestrator. Walks a directory, transforms each supported
|
||||
# file via extract_text, chunks the resulting Markdown via miner.chunk_text,
|
||||
# files drawers via the same lock + purge + upsert pattern convo_miner uses.
|
||||
# Source files on disk are never modified.
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _print_mine_summary(
|
||||
files: list,
|
||||
files_with_text: int,
|
||||
files_skipped: int,
|
||||
files_errored: int,
|
||||
total_drawers: int,
|
||||
status_counts: dict,
|
||||
) -> None:
|
||||
"""Print the post-mine summary block.
|
||||
|
||||
Factored out of ``mine_formats`` to keep the orchestrator under the
|
||||
project's mccabe complexity ceiling (max-complexity=25 in pyproject.toml).
|
||||
No behavior change.
|
||||
"""
|
||||
print(f"\n{'=' * 55}")
|
||||
print(" Summary")
|
||||
print(f"{'-' * 55}")
|
||||
print(f" Files seen: {len(files)}")
|
||||
print(f" Files extracted: {files_with_text}")
|
||||
print(f" Files skipped: {files_skipped}")
|
||||
print(f" Files errored: {files_errored}")
|
||||
print(f" Total drawers: {total_drawers}")
|
||||
if status_counts:
|
||||
print(" Extraction status:")
|
||||
for name, count in sorted(status_counts.items(), key=lambda kv: -kv[1]):
|
||||
print(f" {name:30} {count}")
|
||||
print(f"{'=' * 55}\n")
|
||||
|
||||
|
||||
def _register_skip_sentinel_if_appropriate(
|
||||
collection, source_file: str, wing: str, agent: str, status: ExtractionStatus
|
||||
) -> None:
|
||||
"""Write the ``file_already_mined`` sentinel ONLY when the skip is durable.
|
||||
|
||||
Transient missing-dep statuses (``SKIP_NO_MARKITDOWN``, ``SKIP_NO_STRIPRTF``,
|
||||
``SKIP_MISSING_FORMAT_DEPS``, ``SKIP_NETWORK_TIMEOUT``) intentionally do
|
||||
NOT mark the file as mined — installing the missing extra later (or
|
||||
reconnecting the network) should let the next mine pass pick it up.
|
||||
Per PR #1555 review (Copilot #14).
|
||||
"""
|
||||
if status in _TRANSIENT_MISSING_DEP_STATUSES:
|
||||
return
|
||||
_register_file(collection, source_file, wing, agent)
|
||||
|
||||
|
||||
def _register_file(collection, source_file: str, wing: str, agent: str) -> None:
|
||||
"""Write a sentinel so file_already_mined() returns True for 0-chunk files.
|
||||
|
||||
Without this, files that extract to nothing (or hit a SKIP status) get
|
||||
rescanned on every re-mine. The sentinel preserves the no-op outcome.
|
||||
Mirrors the helper of the same name in convo_miner.py.
|
||||
"""
|
||||
sentinel_id = f"sentinel_{wing}_{hashlib.sha256(source_file.encode()).hexdigest()[:24]}"
|
||||
try:
|
||||
collection.upsert(
|
||||
documents=["[empty]"],
|
||||
ids=[sentinel_id],
|
||||
metadatas=[
|
||||
{
|
||||
"wing": wing,
|
||||
"room": "documents",
|
||||
"source_file": source_file,
|
||||
"chunk_index": -1,
|
||||
"added_by": agent,
|
||||
"filed_at": datetime.now().isoformat(),
|
||||
"ingest_mode": "extract",
|
||||
"extract_mode": "format",
|
||||
"normalize_version": NORMALIZE_VERSION,
|
||||
"is_sentinel": True,
|
||||
}
|
||||
],
|
||||
)
|
||||
except Exception:
|
||||
logger.debug("Sentinel write failed for %s", source_file, exc_info=True)
|
||||
|
||||
|
||||
def _file_chunks_locked(
|
||||
collection,
|
||||
source_file,
|
||||
chunks,
|
||||
wing,
|
||||
room,
|
||||
agent,
|
||||
source_mtime: Optional[float] = None,
|
||||
):
|
||||
"""Lock the source file, purge stale drawers, and upsert fresh chunks.
|
||||
|
||||
Mirrors the canonical convo_miner pattern (locked purge + batched upsert)
|
||||
but with ``ingest_mode="extract"`` so format-mined drawers are
|
||||
distinguishable from project / convo drawers in the palace.
|
||||
|
||||
Each drawer's metadata includes:
|
||||
- ``source_mtime`` — for mtime-based idempotency (file_already_mined
|
||||
with check_mtime=True will re-mine when the source file is updated)
|
||||
- ``hall`` — content-keyword routing (same detect_hall as miner.py)
|
||||
- ``entities`` — extracted entity tags (same _extract_entities_for_metadata)
|
||||
|
||||
Returns ``(drawers_added, skipped)``.
|
||||
"""
|
||||
# Lazy imports to avoid a module-load cycle (miner.py imports from this
|
||||
# module's package, so we defer these helpers until call time).
|
||||
from .miner import _extract_entities_for_metadata, detect_hall
|
||||
|
||||
drawers_added = 0
|
||||
with mine_lock(source_file):
|
||||
# Re-check after lock — another agent may have just finished this file.
|
||||
# Use check_mtime=True so an updated source file is re-mined even
|
||||
# though a prior drawer set exists (matches miner.py semantics).
|
||||
# palace.file_already_mined reads current mtime from disk and compares
|
||||
# against the stored source_mtime metadata, so the caller just toggles
|
||||
# the flag; no need to pass mtime explicitly.
|
||||
# ``extract_mode="format"`` scopes the idempotency check to format-mode
|
||||
# drawers only — drawers from convo_miner / project miner on the same
|
||||
# source_file don't falsely indicate "already mined" to the format
|
||||
# miner. Per PR #1555 review (Copilot #12).
|
||||
if file_already_mined(collection, source_file, check_mtime=True, extract_mode="format"):
|
||||
return 0, True
|
||||
|
||||
try:
|
||||
collection.delete(where={"source_file": source_file})
|
||||
except Exception:
|
||||
logger.debug("Stale-drawer purge failed for %s", source_file, exc_info=True)
|
||||
|
||||
filed_at = datetime.now().isoformat()
|
||||
for batch_start in range(0, len(chunks), DRAWER_UPSERT_BATCH_SIZE):
|
||||
batch_docs: list = []
|
||||
batch_ids: list = []
|
||||
batch_metas: list = []
|
||||
for chunk in chunks[batch_start : batch_start + DRAWER_UPSERT_BATCH_SIZE]:
|
||||
key = (source_file + str(chunk["chunk_index"])).encode()
|
||||
drawer_id = f"drawer_{wing}_{room}_{hashlib.sha256(key).hexdigest()[:24]}"
|
||||
content = chunk["content"]
|
||||
meta: dict = {
|
||||
"wing": wing,
|
||||
"room": room,
|
||||
"source_file": source_file,
|
||||
"chunk_index": chunk["chunk_index"],
|
||||
"added_by": agent,
|
||||
"filed_at": filed_at,
|
||||
"ingest_mode": "extract",
|
||||
"extract_mode": "format",
|
||||
"normalize_version": NORMALIZE_VERSION,
|
||||
"hall": detect_hall(content),
|
||||
}
|
||||
if source_mtime is not None:
|
||||
meta["source_mtime"] = source_mtime
|
||||
entities = _extract_entities_for_metadata(content)
|
||||
if entities:
|
||||
meta["entities"] = entities
|
||||
batch_docs.append(content)
|
||||
batch_ids.append(drawer_id)
|
||||
batch_metas.append(meta)
|
||||
try:
|
||||
collection.upsert(
|
||||
documents=batch_docs,
|
||||
ids=batch_ids,
|
||||
metadatas=batch_metas,
|
||||
)
|
||||
drawers_added += len(batch_docs)
|
||||
except Exception as exc:
|
||||
if "already exists" not in str(exc).lower():
|
||||
raise
|
||||
return drawers_added, False
|
||||
|
||||
|
||||
def mine_formats(
|
||||
format_dir: str,
|
||||
palace_path: str,
|
||||
wing: Optional[str] = None,
|
||||
agent: str = "mempalace",
|
||||
limit: int = 0,
|
||||
dry_run: bool = False,
|
||||
) -> None:
|
||||
"""Mine a directory of binary office-format files into the palace.
|
||||
|
||||
Walks ``format_dir`` via ``scan_formats``, converts each supported file
|
||||
to text via ``extract_text`` (which routes to MarkItDown or striprtf
|
||||
per format), chunks the result with the same chunker miner.py uses, and
|
||||
files the chunks as drawers under the given wing.
|
||||
|
||||
Source files on disk are never modified — conversion is in-memory only.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
format_dir :
|
||||
Directory to walk recursively. Hidden / build dirs are skipped
|
||||
(see ``palace.SKIP_DIRS``). Symlinks are skipped (consistency with
|
||||
``miner.py`` and ``convo_miner.py``). Only files matching
|
||||
``SUPPORTED_FORMATS`` are processed.
|
||||
palace_path :
|
||||
Path to the ChromaDB palace (the destination of the drawers).
|
||||
wing :
|
||||
Wing name. Defaults to the basename of ``format_dir`` (normalized).
|
||||
agent :
|
||||
Identifier recorded in each drawer's ``added_by`` metadata.
|
||||
limit :
|
||||
If > 0, process at most this many files. Useful for sampling.
|
||||
dry_run :
|
||||
If True, walk + extract + chunk but do NOT open the collection or
|
||||
upsert any drawers. Just prints what would have been filed.
|
||||
|
||||
Notes
|
||||
-----
|
||||
- Loads ``MempalaceConfig`` once at the start. The current chunker
|
||||
(``miner.chunk_text``) uses module-level CHUNK_SIZE constants and
|
||||
doesn't accept overrides, so the loaded config values aren't yet
|
||||
threaded into chunking; the load is wired so future ``chunk_text``
|
||||
refactors that accept per-call sizing pick this up automatically.
|
||||
- Each per-file step is wrapped so one malformed file can't crash the
|
||||
whole mine; the loop continues with the next file and logs the
|
||||
offender.
|
||||
- ``KeyboardInterrupt`` is caught at the orchestrator level so the
|
||||
summary still prints on Ctrl-C; partial progress is safe to leave
|
||||
in place because drawer IDs are deterministic (re-mining
|
||||
upserts to the same rows).
|
||||
"""
|
||||
# MempalaceConfig + chunk_text are imported at module level (above) so
|
||||
# tests can patch ``mempalace.format_miner.MempalaceConfig`` and
|
||||
# ``mempalace.format_miner.chunk_text`` directly. Hoisted as part of the
|
||||
# PR #1555 polish PR (Gemini #3).
|
||||
|
||||
# Load palace-wide config. Chunk parameters (chunk_size, chunk_overlap,
|
||||
# min_chunk_size) are now threaded through chunk_text below, so users
|
||||
# who customized their config see the effect in format-mode mining.
|
||||
# Per PR #1555 review (Gemini #3).
|
||||
palace_config = MempalaceConfig()
|
||||
|
||||
format_path = Path(format_dir).expanduser().resolve()
|
||||
if not wing:
|
||||
wing = normalize_wing_name(format_path.name)
|
||||
|
||||
# Load the project's mempalace.yaml (rooms list + wing override) so
|
||||
# detect_room has real categories to route into. Mirrors miner.py:1154.
|
||||
# Fall back to a single "documents" room if no config exists — keeps the
|
||||
# detector well-defined without forcing a yaml on every format dir.
|
||||
try:
|
||||
project_config = load_config(format_dir)
|
||||
rooms = project_config.get(
|
||||
"rooms",
|
||||
[
|
||||
{
|
||||
"name": "documents",
|
||||
"description": "All format-mined files",
|
||||
"keywords": ["documents"],
|
||||
}
|
||||
],
|
||||
)
|
||||
except Exception:
|
||||
logger.debug("mine_formats: load_config fallback to default rooms", exc_info=True)
|
||||
rooms = [
|
||||
{
|
||||
"name": "documents",
|
||||
"description": "All format-mined files",
|
||||
"keywords": ["documents"],
|
||||
}
|
||||
]
|
||||
|
||||
# Initialize loop-state up-front so the outer try/except handlers and the
|
||||
# post-try summary print can run safely even if scan_formats or
|
||||
# get_collection raises before the for loop starts. Per PR #1555 review
|
||||
# (Gemini #5).
|
||||
files: list = []
|
||||
collection = None
|
||||
total_drawers = 0
|
||||
files_skipped = 0
|
||||
files_with_text = 0
|
||||
files_errored = 0
|
||||
status_counts: dict = defaultdict(int)
|
||||
|
||||
try:
|
||||
# Use the resolved ``format_path``, not the raw ``format_dir``, so that
|
||||
# ``~/docs`` and relative inputs work consistently. Per PR #1555 review
|
||||
# (Copilot #10).
|
||||
files = scan_formats(format_path)
|
||||
if limit > 0:
|
||||
files = files[:limit]
|
||||
|
||||
print(f"\n{'=' * 55}")
|
||||
print(" MemPalace Mine — Format extraction")
|
||||
print(f"{'=' * 55}")
|
||||
print(f" Wing: {wing}")
|
||||
print(f" Source: {format_path}")
|
||||
print(f" Files: {len(files)}")
|
||||
print(f" Palace: {palace_path}")
|
||||
if dry_run:
|
||||
print(" DRY RUN — nothing will be filed")
|
||||
print(f"{'-' * 55}\n")
|
||||
|
||||
collection = get_collection(palace_path) if not dry_run else None
|
||||
|
||||
for i, filepath in enumerate(files, 1):
|
||||
source_file = str(filepath)
|
||||
|
||||
# Per-file try/except so one bad file can't crash the whole mine.
|
||||
# Mirrors miner.py's robustness pattern.
|
||||
try:
|
||||
# Cheap mtime read up-front — used as the idempotency key on
|
||||
# subsequent re-mines (check_mtime=True compares stored vs.
|
||||
# current mtime).
|
||||
try:
|
||||
source_mtime: Optional[float] = os.path.getmtime(source_file)
|
||||
except OSError:
|
||||
source_mtime = None
|
||||
|
||||
# Pass extract_mode="format" so format-mode idempotency is
|
||||
# scoped to its own drawer set — drawers from convo_miner /
|
||||
# project miner on the same source_file don't falsely
|
||||
# indicate "already mined" to the format miner. Per PR #1555
|
||||
# review (Copilot #11).
|
||||
if not dry_run and file_already_mined(
|
||||
collection, source_file, check_mtime=True, extract_mode="format"
|
||||
):
|
||||
files_skipped += 1
|
||||
continue
|
||||
|
||||
text, status = extract_text(filepath)
|
||||
status_counts[status.name] += 1
|
||||
|
||||
if status != ExtractionStatus.OK or not text:
|
||||
if not dry_run:
|
||||
_register_skip_sentinel_if_appropriate(
|
||||
collection, source_file, wing, agent, status
|
||||
)
|
||||
print(f" - [{i:4}/{len(files)}] {filepath.name[:50]:50} {status.name}")
|
||||
continue
|
||||
|
||||
# Thread the user's MempalaceConfig chunk parameters through
|
||||
# so format-mode mining honors their tuning. Per PR #1555
|
||||
# review (Gemini #3).
|
||||
chunks = chunk_text(
|
||||
text,
|
||||
source_file,
|
||||
chunk_size=palace_config.chunk_size,
|
||||
chunk_overlap=palace_config.chunk_overlap,
|
||||
min_chunk_size=palace_config.min_chunk_size,
|
||||
)
|
||||
if not chunks:
|
||||
if not dry_run:
|
||||
_register_file(collection, source_file, wing, agent)
|
||||
print(f" - [{i:4}/{len(files)}] {filepath.name[:50]:50} EMPTY_AFTER_CHUNK")
|
||||
continue
|
||||
|
||||
# Route this drawer to a room — same detect_room miner.py
|
||||
# uses (folder match → filename match → content keyword
|
||||
# scoring → fallback "general"). Mirrors miner.py:904.
|
||||
room = detect_room(filepath, text, rooms, format_path)
|
||||
files_with_text += 1
|
||||
|
||||
if dry_run:
|
||||
print(f" [DRY RUN] {filepath.name} → {len(chunks)} drawers")
|
||||
total_drawers += len(chunks)
|
||||
continue
|
||||
|
||||
drawers_added, skipped = _file_chunks_locked(
|
||||
collection,
|
||||
source_file,
|
||||
chunks,
|
||||
wing,
|
||||
room,
|
||||
agent,
|
||||
source_mtime=source_mtime,
|
||||
)
|
||||
if skipped:
|
||||
files_skipped += 1
|
||||
continue
|
||||
|
||||
total_drawers += drawers_added
|
||||
print(f" + [{i:4}/{len(files)}] {filepath.name[:50]:50} +{drawers_added}")
|
||||
except Exception as exc:
|
||||
# Log and continue — one malformed file shouldn't kill the
|
||||
# whole mine. Mirrors miner.py's per-file recovery.
|
||||
files_errored += 1
|
||||
logger.warning(
|
||||
"mine_formats: error processing %s — %s: %s",
|
||||
source_file,
|
||||
type(exc).__name__,
|
||||
str(exc)[:200],
|
||||
)
|
||||
print(
|
||||
f" ! [{i:4}/{len(files)}] {filepath.name[:50]:50} ERROR: {type(exc).__name__}"
|
||||
)
|
||||
continue
|
||||
except KeyboardInterrupt:
|
||||
# Partial progress is safe — deterministic drawer IDs mean a re-mine
|
||||
# upserts to the same rows. Print a clean summary and exit.
|
||||
print("\n Mine interrupted by user (Ctrl-C).")
|
||||
except Exception as exc:
|
||||
# Defense in depth — the per-file try/except above catches most
|
||||
# realistic crashes, but a programming error in the outer-loop
|
||||
# plumbing (file enumeration, scan_formats itself, etc.) would
|
||||
# otherwise propagate as a bare traceback. Catch it here so the
|
||||
# summary still prints and the PID-file finally-block still runs.
|
||||
# Per PR #1555 review (Gemini #5). Mirrors miner.py's belt-and-
|
||||
# suspenders pattern.
|
||||
logger.warning(
|
||||
"mine_formats: unexpected outer-loop exception — %s: %s",
|
||||
type(exc).__name__,
|
||||
str(exc)[:200],
|
||||
)
|
||||
print(
|
||||
f"\n Mine aborted by exception ({type(exc).__name__}: {str(exc)[:120]})",
|
||||
file=sys.stderr,
|
||||
)
|
||||
else:
|
||||
# All files processed without interruption — compute cross-wing topic
|
||||
# tunnels linking this wing to others that share confirmed topics.
|
||||
# Mirrors miner.py:1241-1249 exactly: tunnel-compute failures must
|
||||
# never fail a mine, so any exception is logged and skipped quietly.
|
||||
if not dry_run:
|
||||
try:
|
||||
tunnels_added = _compute_topic_tunnels_for_wing(wing)
|
||||
if tunnels_added:
|
||||
print(f"\n Topic tunnels: +{tunnels_added} cross-wing link(s)")
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"mine_formats: topic tunnel computation skipped — %s: %s",
|
||||
type(exc).__name__,
|
||||
str(exc)[:200],
|
||||
)
|
||||
print(
|
||||
f"\n WARNING: topic tunnel computation skipped — {exc}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
finally:
|
||||
# Hook-spawned mines write a PID file that miner.py's
|
||||
# _cleanup_mine_pid_file() clears; we mirror that so format-mode
|
||||
# mines kicked off the same way don't leave a stale PID behind.
|
||||
try:
|
||||
from .miner import _cleanup_mine_pid_file
|
||||
except ImportError:
|
||||
_cleanup_mine_pid_file = None
|
||||
if _cleanup_mine_pid_file is not None:
|
||||
try:
|
||||
_cleanup_mine_pid_file()
|
||||
except Exception:
|
||||
logger.debug("mine_formats: _cleanup_mine_pid_file failed", exc_info=True)
|
||||
|
||||
_print_mine_summary(
|
||||
files=files,
|
||||
files_with_text=files_with_text,
|
||||
files_skipped=files_skipped,
|
||||
files_errored=files_errored,
|
||||
total_drawers=total_drawers,
|
||||
status_counts=status_counts,
|
||||
)
|
||||
@@ -22,6 +22,8 @@ Usage:
|
||||
import re
|
||||
from typing import List, Dict, Tuple
|
||||
|
||||
from .config import DEFAULT_CHUNK_SIZE
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# MARKER SETS — One per memory type
|
||||
@@ -360,13 +362,23 @@ def _score_markers(text: str, markers: List[str]) -> Tuple[float, List[str]]:
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def extract_memories(text: str, min_confidence: float = 0.3) -> List[Dict]:
|
||||
def extract_memories(
|
||||
text: str,
|
||||
min_confidence: float = 0.3,
|
||||
chunk_size: int = DEFAULT_CHUNK_SIZE,
|
||||
) -> List[Dict]:
|
||||
"""
|
||||
Extract memories from a text string.
|
||||
|
||||
Args:
|
||||
text: The text to extract from (any format).
|
||||
min_confidence: Minimum confidence threshold (0.0-1.0).
|
||||
chunk_size: Per-memory character cap. Segments exceeding this
|
||||
size are sliced verbatim into multiple memories that share
|
||||
the same ``memory_type``. Caller (typically ``mine_convos``)
|
||||
should pass ``MempalaceConfig.chunk_size`` so config-driven
|
||||
sizing reaches this path; the default matches the
|
||||
module-level CHUNK_SIZE in ``convo_miner.py``.
|
||||
|
||||
Returns:
|
||||
List of dicts: {"content": str, "memory_type": str, "chunk_index": int}
|
||||
@@ -410,13 +422,30 @@ def extract_memories(text: str, min_confidence: float = 0.3) -> List[Dict]:
|
||||
if confidence < min_confidence:
|
||||
continue
|
||||
|
||||
memories.append(
|
||||
{
|
||||
"content": para.strip(),
|
||||
"memory_type": max_type,
|
||||
"chunk_index": len(memories),
|
||||
}
|
||||
)
|
||||
content = para.strip()
|
||||
# Slice oversized segments verbatim. ``memory_type`` is propagated
|
||||
# to every slice — reclassifying sub-segments risks dropping data
|
||||
# (marker may live in only one slice), and the verbatim-store
|
||||
# mandate in CLAUDE.md forbids silent loss. Acceptable trade-off:
|
||||
# the label is an approximation for sub-slices, not a fresh
|
||||
# classification.
|
||||
if len(content) <= chunk_size:
|
||||
memories.append(
|
||||
{
|
||||
"content": content,
|
||||
"memory_type": max_type,
|
||||
"chunk_index": len(memories),
|
||||
}
|
||||
)
|
||||
continue
|
||||
for i in range(0, len(content), chunk_size):
|
||||
memories.append(
|
||||
{
|
||||
"content": content[i : i + chunk_size],
|
||||
"memory_type": max_type,
|
||||
"chunk_index": len(memories),
|
||||
}
|
||||
)
|
||||
|
||||
return memories
|
||||
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
"""Hallways — within-wing entity-to-entity connectors.
|
||||
|
||||
A **hallway** is a connection between two entities (people, projects,
|
||||
concepts, interests) inside one wing, materialized from their
|
||||
co-occurrence across that wing's drawers. Conceptually:
|
||||
|
||||
WING → has DRAWERS (each tagged with entities)
|
||||
entities → connected to other entities by HALLWAYS
|
||||
(within-wing, built from drawer co-occurrence)
|
||||
hallways → are the primitive
|
||||
tunnels → use hallways to spawn
|
||||
cross-wing connections
|
||||
|
||||
If Aya and Lumi are both mentioned in 47 drawers across the diary,
|
||||
letters, and ideas rooms, there's a hallway between them. If Aya
|
||||
and "consciousness" co-occur in 19 drawers, there's a hallway between
|
||||
them too. The hallway *is* the structural fact of "these two entities
|
||||
travel together inside this wing."
|
||||
|
||||
Mempalace's tunnel primitive in ``palace_graph.py`` connects rooms
|
||||
across wings. This module fills the within-wing gap with an
|
||||
entity-centric (not room-centric) model: hallways are about *who/what
|
||||
relates to whom/what*, not *which rooms relate to which*. A planned
|
||||
follow-up PR will refactor ``_compute_topic_tunnels_for_wing`` to
|
||||
build cross-wing tunnels from hallway data (Wing → Drawer-entities →
|
||||
Hallway → Tunnel).
|
||||
|
||||
Persistence mirrors ``palace_graph._TUNNEL_FILE``: a JSON file under
|
||||
``~/.mempalace/`` so the records survive across mines and are
|
||||
inspectable / editable by hand if needed.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from collections import defaultdict
|
||||
from datetime import datetime, timezone
|
||||
from itertools import combinations
|
||||
from typing import Optional
|
||||
|
||||
logger = logging.getLogger("mempalace_hallways")
|
||||
|
||||
# Persistence target. Mirrors ``palace_graph._TUNNEL_FILE`` so the storage
|
||||
# pattern is uniform across the two related primitives. Tests override
|
||||
# this via ``monkeypatch.setattr(hallways, "_HALLWAY_FILE", tmp_path/...)``.
|
||||
_HALLWAY_FILE = os.path.join(os.path.expanduser("~"), ".mempalace", "hallways.json")
|
||||
|
||||
_SCHEMA_VERSION = 1
|
||||
|
||||
|
||||
__all__ = [
|
||||
"compute_hallways_for_wing",
|
||||
"list_hallways",
|
||||
"delete_hallway",
|
||||
]
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Persistence — JSON file at _HALLWAY_FILE, restricted perms (0600) on POSIX
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _load_hallways() -> list[dict]:
|
||||
"""Read all hallway records. Returns ``[]`` if the file is missing or corrupt."""
|
||||
if not os.path.exists(_HALLWAY_FILE):
|
||||
return []
|
||||
try:
|
||||
with open(_HALLWAY_FILE, encoding="utf-8") as f:
|
||||
raw = json.load(f)
|
||||
except (OSError, json.JSONDecodeError):
|
||||
logger.debug("hallways: load failed, treating as empty", exc_info=True)
|
||||
return []
|
||||
if isinstance(raw, dict) and "hallways" in raw:
|
||||
return raw.get("hallways") or []
|
||||
if isinstance(raw, list):
|
||||
return raw
|
||||
return []
|
||||
|
||||
|
||||
def _save_hallways(hallways: list[dict]) -> None:
|
||||
"""Atomically persist hallway records to _HALLWAY_FILE.
|
||||
|
||||
Uses an os.replace temp-file dance so a crash mid-write doesn't
|
||||
corrupt the file. POSIX permission is restricted to 0600 because
|
||||
hallways reveal within-wing entity connections that the user may
|
||||
not want world-readable.
|
||||
"""
|
||||
directory = os.path.dirname(_HALLWAY_FILE)
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
payload = {
|
||||
"schema_version": _SCHEMA_VERSION,
|
||||
"hallways": list(hallways),
|
||||
}
|
||||
fd, tmp_path = tempfile.mkstemp(prefix=".hallways-", suffix=".tmp", dir=directory)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
||||
json.dump(payload, f, indent=2, ensure_ascii=False)
|
||||
try:
|
||||
os.chmod(tmp_path, 0o600)
|
||||
except OSError:
|
||||
# Non-POSIX systems may not support chmod; not fatal.
|
||||
pass
|
||||
os.replace(tmp_path, _HALLWAY_FILE)
|
||||
except Exception:
|
||||
try:
|
||||
os.unlink(tmp_path)
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Core algorithm — compute entity-pair hallways for one wing
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _parse_entities(value) -> list[str]:
|
||||
"""Drawer ``entities`` metadata is a semicolon-separated string. Parse it.
|
||||
|
||||
Returns a deterministic *list* (not a set) because order matters for
|
||||
the deduplication semantics below: a drawer that mentions ``Aya;Aya``
|
||||
should only contribute one Aya to the entity set for that drawer.
|
||||
"""
|
||||
if not value:
|
||||
return []
|
||||
if isinstance(value, (list, tuple, set)):
|
||||
items = [str(v).strip() for v in value if str(v).strip()]
|
||||
elif isinstance(value, str):
|
||||
items = [v.strip() for v in value.split(";") if v.strip()]
|
||||
else:
|
||||
return []
|
||||
# Dedupe while preserving first-seen order so id derivation is stable.
|
||||
seen: set[str] = set()
|
||||
result: list[str] = []
|
||||
for item in items:
|
||||
if item not in seen:
|
||||
seen.add(item)
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
||||
def _hallway_id(wing: str, entity_a: str, entity_b: str) -> str:
|
||||
"""Deterministic id derived from wing + sorted entity pair.
|
||||
|
||||
Sorting before hashing makes the id symmetric — (Aya, Lumi) and
|
||||
(Lumi, Aya) produce the same record. So an idempotent re-mine
|
||||
upserts the same hallway instead of creating two parallel records.
|
||||
"""
|
||||
a, b = sorted([entity_a, entity_b])
|
||||
key = f"{wing}::{a}::{b}".encode("utf-8")
|
||||
suffix = hashlib.sha256(key).hexdigest()[:8]
|
||||
return f"hallway_{wing}_{a}_{b}_{suffix}"
|
||||
|
||||
|
||||
def compute_hallways_for_wing(
|
||||
wing: str,
|
||||
col=None,
|
||||
min_count: int = 2,
|
||||
) -> list[dict]:
|
||||
"""Compute entity-pair hallways for one wing.
|
||||
|
||||
Algorithm:
|
||||
1. Query drawers for ``wing`` from ``col``.
|
||||
2. For each drawer with entities, every pair of distinct entities in
|
||||
that drawer is one co-occurrence. Increment a counter for each
|
||||
pair; also record the room the drawer lives in.
|
||||
3. For each (entity_a, entity_b) pair whose co-occurrence count is
|
||||
``>= min_count``, materialize a hallway record. The record
|
||||
carries the pair, the count, and the set of rooms where they
|
||||
co-occurred (useful context for navigation).
|
||||
4. Persist the full hallway list (records for other wings preserved,
|
||||
this wing's records replaced) and return the just-computed list.
|
||||
|
||||
Args:
|
||||
wing: wing name to scan.
|
||||
col: ChromaDB collection — must support ``.get(where=..., include=...)``.
|
||||
If ``None``, returns ``[]`` (caller didn't supply a backing
|
||||
store, so nothing to compute against). Tests pass a controlled
|
||||
MagicMock.
|
||||
min_count: minimum co-occurrence count required to materialize a
|
||||
hallway between two entities. Default 2 — single co-occurrences
|
||||
are noise (entities mentioned together once in one drawer);
|
||||
two or more is a real signal. Clamped to ``>=1``.
|
||||
|
||||
Returns:
|
||||
List of hallway dicts created for this wing. Records for other
|
||||
wings already on disk are preserved.
|
||||
"""
|
||||
if col is None:
|
||||
logger.debug("compute_hallways_for_wing: no collection provided for %s", wing)
|
||||
return []
|
||||
|
||||
min_count = max(1, int(min_count))
|
||||
|
||||
# 1. Query drawers for this wing.
|
||||
try:
|
||||
results = col.get(where={"wing": wing}, include=["metadatas"])
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"compute_hallways_for_wing: collection.get failed for %s", wing, exc_info=True
|
||||
)
|
||||
return []
|
||||
|
||||
metadatas = (results or {}).get("metadatas") or []
|
||||
if not metadatas:
|
||||
return []
|
||||
|
||||
# 2. Walk drawers, counting entity-pair co-occurrence + tracking rooms.
|
||||
# pair_counts: {(entity_a, entity_b): count} — keys always sorted to
|
||||
# canonicalize the (a, b) vs (b, a) symmetry.
|
||||
pair_counts: dict[tuple[str, str], int] = defaultdict(int)
|
||||
pair_rooms: dict[tuple[str, str], set[str]] = defaultdict(set)
|
||||
|
||||
for meta in metadatas:
|
||||
if not isinstance(meta, dict):
|
||||
continue
|
||||
# Sentinel drawers carry no real content — skip them.
|
||||
if meta.get("is_sentinel"):
|
||||
continue
|
||||
entities = _parse_entities(meta.get("entities"))
|
||||
if len(entities) < 2:
|
||||
# Need at least 2 entities for a pair to exist.
|
||||
continue
|
||||
room = meta.get("room")
|
||||
room_str = room if isinstance(room, str) and room.strip() else None
|
||||
|
||||
# Each unordered pair of distinct entities in this drawer is one
|
||||
# co-occurrence. itertools.combinations already gives unordered
|
||||
# pairs without repetition.
|
||||
for a, b in combinations(entities, 2):
|
||||
# Canonicalize order so (Aya, Lumi) and (Lumi, Aya) are the
|
||||
# same key. Skip self-pairs defensively.
|
||||
if a == b:
|
||||
continue
|
||||
key = tuple(sorted([a, b]))
|
||||
pair_counts[key] += 1
|
||||
if room_str:
|
||||
pair_rooms[key].add(room_str)
|
||||
|
||||
if not pair_counts:
|
||||
return []
|
||||
|
||||
# 3. Materialize hallway records for pairs above the threshold.
|
||||
created: list[dict] = []
|
||||
created_at = datetime.now(timezone.utc).isoformat()
|
||||
for key in sorted(pair_counts.keys()):
|
||||
count = pair_counts[key]
|
||||
if count < min_count:
|
||||
continue
|
||||
entity_a, entity_b = key
|
||||
rooms = sorted(pair_rooms.get(key, set()))
|
||||
room_summary = ", ".join(rooms[:3]) if rooms else "(no room tags)"
|
||||
if len(rooms) > 3:
|
||||
room_summary += f", +{len(rooms) - 3} more"
|
||||
created.append(
|
||||
{
|
||||
"id": _hallway_id(wing, entity_a, entity_b),
|
||||
"wing": wing,
|
||||
"entity_a": entity_a,
|
||||
"entity_b": entity_b,
|
||||
"co_occurrence_count": count,
|
||||
"rooms": rooms,
|
||||
"label": f"{entity_a} ↔ {entity_b} (co-occur in {count} drawers across {len(rooms) or 'no'} room{'s' if len(rooms) != 1 else ''}: {room_summary})",
|
||||
"created_at": created_at,
|
||||
"created_by": "auto",
|
||||
}
|
||||
)
|
||||
|
||||
# 4. Persist — preserve other-wing records, replace this wing's records.
|
||||
existing = _load_hallways()
|
||||
preserved = [h for h in existing if h.get("wing") != wing]
|
||||
_save_hallways(preserved + created)
|
||||
|
||||
return created
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Query API — list_hallways, delete_hallway
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def list_hallways(wing: Optional[str] = None) -> list[dict]:
|
||||
"""List hallway records. Filter by ``wing`` if specified."""
|
||||
all_hallways = _load_hallways()
|
||||
if wing is None:
|
||||
return list(all_hallways)
|
||||
return [h for h in all_hallways if h.get("wing") == wing]
|
||||
|
||||
|
||||
def delete_hallway(hallway_id: str) -> bool:
|
||||
"""Remove one hallway record by id. Returns True if a record was removed."""
|
||||
hallways = _load_hallways()
|
||||
filtered = [h for h in hallways if h.get("id") != hallway_id]
|
||||
if len(filtered) == len(hallways):
|
||||
return False
|
||||
_save_hallways(filtered)
|
||||
return True
|
||||
+90
-10
@@ -12,6 +12,7 @@ import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -281,6 +282,31 @@ _MINE_PID_DIR = STATE_DIR / "mine_pids"
|
||||
# own slot on exit without scanning the whole directory.
|
||||
_MINE_PID_FILE_ENV = "MEMPALACE_MINE_PID_FILE"
|
||||
|
||||
# Maximum wall-clock hours a mine subprocess is allowed to run before its
|
||||
# PID slot is treated as stale (even if the process is still alive). A
|
||||
# wedged mine — e.g. one that is blocking indefinitely on ChromaDB
|
||||
# cold-init under concurrent Windows load (#1552) — would otherwise hold
|
||||
# its slot forever. Set MEMPALACE_MINE_TIMEOUT_HOURS=0 to disable the
|
||||
# timeout (slots are reclaimed only when the PID is dead).
|
||||
_MINE_TIMEOUT_HOURS_ENV = "MEMPALACE_MINE_TIMEOUT_HOURS"
|
||||
_MINE_TIMEOUT_HOURS_DEFAULT = 2.0
|
||||
|
||||
|
||||
def _mine_slot_timeout_secs() -> float:
|
||||
"""Return the configured mine-slot timeout in seconds.
|
||||
|
||||
Reads ``MEMPALACE_MINE_TIMEOUT_HOURS`` from the environment (float).
|
||||
Returns 0 if the env var is set to 0 or is not parseable.
|
||||
"""
|
||||
raw = os.environ.get(_MINE_TIMEOUT_HOURS_ENV, "")
|
||||
if raw:
|
||||
try:
|
||||
hours = float(raw)
|
||||
return max(0.0, hours) * 3600
|
||||
except ValueError:
|
||||
return 0.0
|
||||
return _MINE_TIMEOUT_HOURS_DEFAULT * 3600
|
||||
|
||||
|
||||
def _pid_file_for_cmd(cmd: list[str]) -> Path:
|
||||
"""Return the per-target PID file path for a mine subcommand.
|
||||
@@ -333,15 +359,70 @@ def _pid_alive(pid: int) -> bool:
|
||||
|
||||
|
||||
def _mine_already_running(cmd: list[str]) -> bool:
|
||||
"""Return True if a previous mine for ``cmd``'s target is still alive."""
|
||||
"""Return True if a previous mine for ``cmd``'s target is still alive.
|
||||
|
||||
The PID file format is ``{pid} {unix_timestamp}`` (timestamp added in
|
||||
#1552 to detect wedged subprocesses). Old-format files (bare ``{pid}``)
|
||||
use the PID file's mtime as the approximate start time so a still-running
|
||||
pre-upgrade mine is not immediately misclassified as stale.
|
||||
|
||||
A process is considered stale (and this function returns False) when:
|
||||
- the PID is dead, OR
|
||||
- the configured mine timeout is > 0 AND the process has been running
|
||||
longer than the timeout.
|
||||
"""
|
||||
pid_file = _pid_file_for_cmd(cmd)
|
||||
try:
|
||||
recorded = pid_file.read_text().strip()
|
||||
except OSError:
|
||||
return False
|
||||
if not recorded.isdigit():
|
||||
if not recorded:
|
||||
return False
|
||||
return _pid_alive(int(recorded))
|
||||
parts = recorded.split(None, 1)
|
||||
if not parts[0].isdigit():
|
||||
return False
|
||||
pid = int(parts[0])
|
||||
if not _pid_alive(pid):
|
||||
return False
|
||||
timeout_secs = _mine_slot_timeout_secs()
|
||||
if timeout_secs > 0:
|
||||
if len(parts) > 1 and parts[1]:
|
||||
try:
|
||||
start_ts = float(parts[1])
|
||||
except ValueError:
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
start_ts = pid_file.stat().st_mtime
|
||||
except OSError:
|
||||
return True
|
||||
if time.time() - start_ts > timeout_secs:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _create_mine_slot_with_placeholder(pid_file: Path) -> Path:
|
||||
"""Atomically create a mine PID slot and write this hook PID into it.
|
||||
|
||||
The slot body is ``{pid} {unix_timestamp}`` so that stale-by-age
|
||||
detection in ``_mine_already_running`` can determine how long the
|
||||
recorded process has been running (#1552).
|
||||
"""
|
||||
fd = os.open(str(pid_file), os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="ascii") as f:
|
||||
f.write(f"{os.getpid()} {int(time.time())}")
|
||||
except OSError:
|
||||
try:
|
||||
os.close(fd)
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
pid_file.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
return pid_file
|
||||
|
||||
|
||||
def _claim_mine_slot(cmd: list[str]) -> Optional[Path]:
|
||||
@@ -359,14 +440,14 @@ def _claim_mine_slot(cmd: list[str]) -> Optional[Path]:
|
||||
pid_file = _pid_file_for_cmd(cmd)
|
||||
pid_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
fd = os.open(str(pid_file), os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
|
||||
os.close(fd)
|
||||
return pid_file
|
||||
return _create_mine_slot_with_placeholder(pid_file)
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
# Slot exists. If the holder is alive, defer.
|
||||
if _mine_already_running(cmd):
|
||||
return None
|
||||
|
||||
# Stale entry; reclaim. The unlink+create is racy against another hook
|
||||
# firing right now, but the second create's O_EXCL will fail and that
|
||||
# caller will see the live PID via the next round.
|
||||
@@ -376,10 +457,9 @@ def _claim_mine_slot(cmd: list[str]) -> Optional[Path]:
|
||||
pass
|
||||
except OSError:
|
||||
return None
|
||||
|
||||
try:
|
||||
fd = os.open(str(pid_file), os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
|
||||
os.close(fd)
|
||||
return pid_file
|
||||
return _create_mine_slot_with_placeholder(pid_file)
|
||||
except FileExistsError:
|
||||
return None
|
||||
|
||||
@@ -419,7 +499,7 @@ def _spawn_mine(cmd: list) -> None:
|
||||
pass
|
||||
raise
|
||||
try:
|
||||
pid_file.write_text(str(proc.pid))
|
||||
pid_file.write_text(f"{proc.pid} {int(time.time())}")
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
+236
-51
@@ -176,6 +176,27 @@ _kg_by_path: dict[str, KnowledgeGraph] = {}
|
||||
_kg_cache_lock = threading.Lock()
|
||||
_palace_flag_given: bool = bool(_args.palace)
|
||||
|
||||
# MCP server idle auto-exit (#1552). Stale MCP servers from ended Claude
|
||||
# Code sessions do not self-terminate, accumulating ChromaDB/HNSW file
|
||||
# handles on Windows. When MEMPALACE_MCP_IDLE_HOURS is set (or defaults
|
||||
# to 8 h), a background daemon thread exits the process once no request
|
||||
# has been handled for that long. Set to 0 to disable.
|
||||
_MCP_IDLE_HOURS_ENV = "MEMPALACE_MCP_IDLE_HOURS"
|
||||
_MCP_IDLE_HOURS_DEFAULT = 8.0
|
||||
_last_request_time: float = time.monotonic()
|
||||
|
||||
|
||||
def _mcp_idle_timeout_secs() -> float:
|
||||
"""Return the configured MCP idle timeout in seconds (0 = disabled)."""
|
||||
raw = os.environ.get(_MCP_IDLE_HOURS_ENV, "")
|
||||
if raw:
|
||||
try:
|
||||
hours = float(raw)
|
||||
return max(0.0, hours) * 3600
|
||||
except ValueError:
|
||||
return 0.0
|
||||
return _MCP_IDLE_HOURS_DEFAULT * 3600
|
||||
|
||||
|
||||
def _resolve_kg_path() -> str:
|
||||
if _palace_flag_given:
|
||||
@@ -1026,22 +1047,27 @@ def tool_create_tunnel(
|
||||
Example: an API design discussion in project_api connects to the
|
||||
database schema in project_database.
|
||||
"""
|
||||
# sanitize_name and create_tunnel both raise ValueError for invalid or
|
||||
# missing endpoints (empty/non-string names, and create_tunnel's
|
||||
# room-existence checks). Catch both so the real reason is surfaced
|
||||
# instead of escaping and being wrapped as the opaque "Internal tool
|
||||
# error" (#1473), mirroring sibling tools.
|
||||
try:
|
||||
source_wing = sanitize_name(source_wing, "source_wing")
|
||||
source_room = sanitize_name(source_room, "source_room")
|
||||
target_wing = sanitize_name(target_wing, "target_wing")
|
||||
target_room = sanitize_name(target_room, "target_room")
|
||||
return create_tunnel(
|
||||
source_wing,
|
||||
source_room,
|
||||
target_wing,
|
||||
target_room,
|
||||
label=label,
|
||||
source_drawer_id=source_drawer_id,
|
||||
target_drawer_id=target_drawer_id,
|
||||
)
|
||||
except ValueError as e:
|
||||
return {"error": str(e)}
|
||||
return create_tunnel(
|
||||
source_wing,
|
||||
source_room,
|
||||
target_wing,
|
||||
target_room,
|
||||
label=label,
|
||||
source_drawer_id=source_drawer_id,
|
||||
target_drawer_id=target_drawer_id,
|
||||
)
|
||||
|
||||
|
||||
def tool_list_tunnels(wing: str = None):
|
||||
@@ -1077,7 +1103,18 @@ def tool_follow_tunnels(wing: str, room: str):
|
||||
def tool_add_drawer(
|
||||
wing: str, room: str, content: str, source_file: str = None, added_by: str = "mcp"
|
||||
):
|
||||
"""File verbatim content into a wing/room. Checks for duplicates first."""
|
||||
"""File verbatim content into a wing/room. Checks for duplicates first.
|
||||
|
||||
Content above ``chunk_size`` is split into bounded per-chunk drawers
|
||||
via a single batched upsert. Each chunk carries ``parent_drawer_id``
|
||||
linkage and ``chunk_index`` metadata so search can rejoin them. The
|
||||
returned ``drawer_id`` is the LOGICAL group handle on the chunked
|
||||
path; physical drawer ids are in ``chunk_ids`` (#1539). To delete
|
||||
or fetch the underlying drawers, iterate ``chunk_ids`` or query by
|
||||
``parent_drawer_id`` — ``tool_get_drawer(drawer_id)`` and
|
||||
``tool_delete_drawer(drawer_id)`` report "not found" on the chunked
|
||||
path because no row is stored under the logical group id.
|
||||
"""
|
||||
global _metadata_cache
|
||||
try:
|
||||
wing = sanitize_name(wing, "wing")
|
||||
@@ -1106,38 +1143,93 @@ def tool_add_drawer(
|
||||
},
|
||||
)
|
||||
|
||||
# Idempotency: if the deterministic ID already exists, return success as a no-op.
|
||||
chunk_size = _config.chunk_size
|
||||
base_meta = {
|
||||
"wing": wing,
|
||||
"room": room,
|
||||
"source_file": source_file or "",
|
||||
"added_by": added_by,
|
||||
"filed_at": datetime.now().isoformat(),
|
||||
}
|
||||
|
||||
# Idempotency. Three cases to detect a prior committed write:
|
||||
# (a) Single-doc path: drawer_id row exists (the only id used).
|
||||
# (b) Chunked path: probe the LAST chunk id — its presence implies
|
||||
# every earlier chunk also landed, since the batched upsert
|
||||
# is all-or-nothing.
|
||||
# (c) Legacy pre-#1539 single-row write of oversized content under
|
||||
# drawer_id: probe drawer_id alongside the last chunk id so a
|
||||
# re-call with identical oversized content does not duplicate
|
||||
# the legacy row by adding fresh chunks under different ids.
|
||||
if len(content) <= chunk_size:
|
||||
idempotency_probe_ids = [drawer_id]
|
||||
else:
|
||||
last_chunk_idx = (len(content) - 1) // chunk_size
|
||||
idempotency_probe_ids = [drawer_id, f"{drawer_id}_chunk_{last_chunk_idx:06d}"]
|
||||
try:
|
||||
existing = col.get(ids=[drawer_id], include=[])
|
||||
existing = col.get(ids=idempotency_probe_ids, include=[])
|
||||
if existing.ids:
|
||||
return {"success": True, "reason": "already_exists", "drawer_id": drawer_id}
|
||||
except Exception:
|
||||
logger.debug("Idempotency pre-check failed for %s", drawer_id, exc_info=True)
|
||||
logger.debug("Idempotency pre-check failed for %s", idempotency_probe_ids, exc_info=True)
|
||||
|
||||
try:
|
||||
col.upsert(
|
||||
ids=[drawer_id],
|
||||
documents=[content],
|
||||
metadatas=[
|
||||
{
|
||||
"wing": wing,
|
||||
"room": room,
|
||||
"source_file": source_file or "",
|
||||
"chunk_index": 0,
|
||||
"added_by": added_by,
|
||||
"filed_at": datetime.now().isoformat(),
|
||||
}
|
||||
],
|
||||
)
|
||||
inserted = col.get(ids=[drawer_id], include=[])
|
||||
if len(content) <= chunk_size:
|
||||
col.upsert(
|
||||
ids=[drawer_id],
|
||||
documents=[content],
|
||||
metadatas=[{**base_meta, "chunk_index": 0}],
|
||||
)
|
||||
inserted = col.get(ids=[drawer_id], include=[])
|
||||
if not inserted.ids:
|
||||
raise RuntimeError(
|
||||
"Drawer write was acknowledged but the new ID is not readable. "
|
||||
"The palace index may be stale; run reconnect or repair."
|
||||
)
|
||||
_metadata_cache = None
|
||||
logger.info(f"Filed drawer: {drawer_id} → {wing}/{room}")
|
||||
return {
|
||||
"success": True,
|
||||
"drawer_id": drawer_id,
|
||||
"wing": wing,
|
||||
"room": room,
|
||||
"chunks": 1,
|
||||
}
|
||||
|
||||
# Oversized content: split into bounded per-chunk drawers so the
|
||||
# embedding model never sees a document above ``chunk_size``.
|
||||
# Single batched ``upsert`` so the embedding pass either commits
|
||||
# every chunk or none — no half-written palace if the embedding
|
||||
# model fails mid-loop (#1539).
|
||||
chunk_ids: list[str] = []
|
||||
chunk_docs: list[str] = []
|
||||
chunk_metas: list[dict] = []
|
||||
for i in range(0, len(content), chunk_size):
|
||||
chunk_idx = i // chunk_size
|
||||
chunk_ids.append(f"{drawer_id}_chunk_{chunk_idx:06d}")
|
||||
chunk_docs.append(content[i : i + chunk_size])
|
||||
chunk_metas.append(
|
||||
{**base_meta, "chunk_index": chunk_idx, "parent_drawer_id": drawer_id}
|
||||
)
|
||||
col.upsert(ids=chunk_ids, documents=chunk_docs, metadatas=chunk_metas)
|
||||
# Probe the LAST chunk id, not the first — its presence confirms
|
||||
# the whole batch landed, not just the leading row.
|
||||
inserted = col.get(ids=[chunk_ids[-1]], include=[])
|
||||
if not inserted.ids:
|
||||
raise RuntimeError(
|
||||
"Drawer write was acknowledged but the new ID is not readable. "
|
||||
"The palace index may be stale; run reconnect or repair."
|
||||
)
|
||||
_metadata_cache = None
|
||||
logger.info(f"Filed drawer: {drawer_id} → {wing}/{room}")
|
||||
return {"success": True, "drawer_id": drawer_id, "wing": wing, "room": room}
|
||||
logger.info(f"Filed drawer: {drawer_id} → {wing}/{room} ({len(chunk_ids)} chunks)")
|
||||
return {
|
||||
"success": True,
|
||||
"drawer_id": drawer_id,
|
||||
"wing": wing,
|
||||
"room": room,
|
||||
"chunks": len(chunk_ids),
|
||||
"chunk_ids": chunk_ids,
|
||||
}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@@ -1199,7 +1291,11 @@ def tool_sync(project_dir: str = None, wing: str = None, apply: bool = False):
|
||||
# below, otherwise MineAlreadyRunning and ValueError fall into the
|
||||
# generic "sync failed" branch and break the structured-error tests.
|
||||
except MineAlreadyRunning as exc:
|
||||
return {"success": False, "error": f"another mine is in progress: {exc}"}
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"another mine is in progress: {exc}",
|
||||
"error_class": "LockHeldByOtherProcess",
|
||||
}
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as exc:
|
||||
@@ -1549,29 +1645,76 @@ def tool_diary_write(agent_name: str, entry: str, topic: str = "general", wing:
|
||||
# semantic search quality. For now, store raw AAAK in metadata so it's
|
||||
# preserved, and keep the document as-is for embedding (even though
|
||||
# compressed AAAK degrades embedding quality).
|
||||
col.add(
|
||||
ids=[entry_id],
|
||||
documents=[entry],
|
||||
metadatas=[
|
||||
base_metadata = {
|
||||
"wing": wing,
|
||||
"room": room,
|
||||
"hall": "hall_diary",
|
||||
"topic": topic,
|
||||
"type": "diary_entry",
|
||||
"agent": agent_name,
|
||||
"filed_at": now.isoformat(),
|
||||
"date": now.strftime("%Y-%m-%d"),
|
||||
}
|
||||
chunk_size = _config.chunk_size
|
||||
if len(entry) <= chunk_size:
|
||||
col.add(
|
||||
ids=[entry_id],
|
||||
documents=[entry],
|
||||
metadatas=[{**base_metadata, "chunk_index": 0}],
|
||||
)
|
||||
logger.info(f"Diary entry: {entry_id} → {wing}/diary/{topic}")
|
||||
return {
|
||||
"success": True,
|
||||
"entry_id": entry_id,
|
||||
"agent": agent_name,
|
||||
"topic": topic,
|
||||
"timestamp": now.isoformat(),
|
||||
"chunks": 1,
|
||||
}
|
||||
|
||||
# Oversized entry: split into bounded per-chunk drawers so the
|
||||
# embedding model never sees a document above ``chunk_size``.
|
||||
# Every chunk carries ``parent_entry_id`` so search can rejoin
|
||||
# them and ``chunk_index`` for ordered reconstruction (#1539).
|
||||
# Note on ``entry_id`` in the return value: for the chunked
|
||||
# path the returned ``entry_id`` is the LOGICAL group handle
|
||||
# (no drawer is stored under that exact id). The physical
|
||||
# drawer ids are in ``chunk_ids``. Callers wanting to fetch
|
||||
# by id should iterate ``chunk_ids``; callers wanting to
|
||||
# query by metadata can filter on ``parent_entry_id``.
|
||||
# Use a single batched ``add`` so the embedding pass either
|
||||
# commits all chunks or none — avoids a half-written palace
|
||||
# if the embedding model fails mid-loop. ``col.add`` (not
|
||||
# ``upsert``) is intentional here: ``entry_id`` is timestamp-
|
||||
# based with microsecond precision, so every call generates a
|
||||
# fresh id and a duplicate is by definition a same-microsecond
|
||||
# clash that should surface as an error rather than silently
|
||||
# overwrite the prior entry (cf. ``tool_add_drawer`` whose
|
||||
# content-hash ids are deliberately idempotent and use upsert).
|
||||
chunk_ids: list[str] = []
|
||||
chunk_docs: list[str] = []
|
||||
chunk_metas: list[dict] = []
|
||||
for i in range(0, len(entry), chunk_size):
|
||||
chunk_idx = i // chunk_size
|
||||
chunk_ids.append(f"{entry_id}_chunk_{chunk_idx:06d}")
|
||||
chunk_docs.append(entry[i : i + chunk_size])
|
||||
chunk_metas.append(
|
||||
{
|
||||
"wing": wing,
|
||||
"room": room,
|
||||
"hall": "hall_diary",
|
||||
"topic": topic,
|
||||
"type": "diary_entry",
|
||||
"agent": agent_name,
|
||||
"filed_at": now.isoformat(),
|
||||
"date": now.strftime("%Y-%m-%d"),
|
||||
**base_metadata,
|
||||
"chunk_index": chunk_idx,
|
||||
"parent_entry_id": entry_id,
|
||||
}
|
||||
],
|
||||
)
|
||||
logger.info(f"Diary entry: {entry_id} → {wing}/diary/{topic}")
|
||||
)
|
||||
col.add(ids=chunk_ids, documents=chunk_docs, metadatas=chunk_metas)
|
||||
logger.info(f"Diary entry: {entry_id} → {wing}/diary/{topic} ({len(chunk_ids)} chunks)")
|
||||
return {
|
||||
"success": True,
|
||||
"entry_id": entry_id,
|
||||
"agent": agent_name,
|
||||
"topic": topic,
|
||||
"timestamp": now.isoformat(),
|
||||
"chunks": len(chunk_ids),
|
||||
"chunk_ids": chunk_ids,
|
||||
}
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
@@ -2281,22 +2424,30 @@ SUPPORTED_PROTOCOL_VERSIONS = [
|
||||
]
|
||||
|
||||
|
||||
def _internal_tool_error(req_id, tool_name: str) -> dict:
|
||||
def _internal_tool_error(req_id, tool_name: str, exc: BaseException = None) -> dict:
|
||||
logger.exception(f"Tool error in {tool_name}")
|
||||
error: dict = {"code": -32000, "message": "Internal tool error"}
|
||||
if exc is not None:
|
||||
error["data"] = {
|
||||
"error_class": type(exc).__name__,
|
||||
"message": str(exc),
|
||||
}
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"error": {"code": -32000, "message": "Internal tool error"},
|
||||
"error": error,
|
||||
}
|
||||
|
||||
|
||||
def handle_request(request):
|
||||
global _last_request_time
|
||||
if not isinstance(request, dict):
|
||||
return {
|
||||
"jsonrpc": "2.0",
|
||||
"id": None,
|
||||
"error": {"code": -32600, "message": "Invalid Request"},
|
||||
}
|
||||
_last_request_time = time.monotonic()
|
||||
method = request.get("method") or ""
|
||||
params = request.get("params") or {}
|
||||
req_id = request.get("id")
|
||||
@@ -2444,9 +2595,9 @@ def handle_request(request):
|
||||
"message": f"Missing required {word} {quoted} for tool {tool_name}",
|
||||
},
|
||||
}
|
||||
return _internal_tool_error(req_id, tool_name)
|
||||
except Exception:
|
||||
return _internal_tool_error(req_id, tool_name)
|
||||
return _internal_tool_error(req_id, tool_name, e)
|
||||
except Exception as exc:
|
||||
return _internal_tool_error(req_id, tool_name, exc)
|
||||
|
||||
# Notifications (missing id) must never get a response
|
||||
if req_id is None:
|
||||
@@ -2621,6 +2772,37 @@ def _maybe_eager_warmup_embedder() -> None:
|
||||
)
|
||||
|
||||
|
||||
def _start_idle_exit_watchdog() -> None:
|
||||
"""Start a daemon thread that exits the process after an idle period.
|
||||
|
||||
When no request has been handled for ``MEMPALACE_MCP_IDLE_HOURS``
|
||||
(default 8 h), the thread terminates the process so that stale MCP
|
||||
servers from ended Claude Code sessions do not accumulate ChromaDB /
|
||||
HNSW file handles on Windows (#1552).
|
||||
|
||||
Set ``MEMPALACE_MCP_IDLE_HOURS=0`` to disable the watchdog.
|
||||
"""
|
||||
timeout = _mcp_idle_timeout_secs()
|
||||
if timeout <= 0:
|
||||
return
|
||||
check_interval = min(60.0, timeout / 4)
|
||||
|
||||
def _watchdog() -> None:
|
||||
while True:
|
||||
time.sleep(check_interval)
|
||||
idle = time.monotonic() - _last_request_time
|
||||
if idle >= timeout:
|
||||
logger.info(
|
||||
"MCP server idle for %.1f h (limit %.1f h); exiting to release file handles.",
|
||||
idle / 3600,
|
||||
timeout / 3600,
|
||||
)
|
||||
os._exit(0)
|
||||
|
||||
t = threading.Thread(target=_watchdog, name="mcp-idle-watchdog", daemon=True)
|
||||
t.start()
|
||||
|
||||
|
||||
def main():
|
||||
"""MCP server entry point for the ``mempalace-mcp`` console script.
|
||||
|
||||
@@ -2656,6 +2838,9 @@ def main():
|
||||
# does not pay the ONNX/CoreML cold-load tax under the MCP client
|
||||
# timeout (#1495). Default off — preserves current startup latency.
|
||||
_maybe_eager_warmup_embedder()
|
||||
# Idle auto-exit: release ChromaDB file handles from stale servers
|
||||
# that outlived their Claude Code session (#1552).
|
||||
_start_idle_exit_watchdog()
|
||||
while True:
|
||||
try:
|
||||
line = sys.stdin.readline()
|
||||
|
||||
+108
-92
@@ -22,8 +22,10 @@ import errno
|
||||
import os
|
||||
import shutil
|
||||
import sqlite3
|
||||
import tempfile
|
||||
import uuid
|
||||
from collections import defaultdict
|
||||
from contextlib import closing
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
@@ -52,61 +54,65 @@ def extract_drawers_from_sqlite(db_path: str) -> list:
|
||||
|
||||
Works regardless of which ChromaDB version created the database.
|
||||
Returns list of dicts with 'id', 'document', and 'metadata' keys.
|
||||
|
||||
The connection is wrapped in ``contextlib.closing`` so an exception
|
||||
during extraction does not leak the SQLite handle. On Windows that
|
||||
would leave a file lock on ``chroma.sqlite3`` and prevent the rest
|
||||
of the migration from touching the palace directory.
|
||||
"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
with closing(sqlite3.connect(db_path)) as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
||||
# Get all embedding IDs and their documents
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT e.embedding_id,
|
||||
MAX(CASE WHEN em.key = 'chroma:document' THEN em.string_value END) as document
|
||||
FROM embeddings e
|
||||
JOIN embedding_metadata em ON em.id = e.id
|
||||
GROUP BY e.embedding_id
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
drawers = []
|
||||
for row in rows:
|
||||
embedding_id = row["embedding_id"]
|
||||
document = row["document"]
|
||||
if not document:
|
||||
continue
|
||||
|
||||
# Get metadata for this embedding
|
||||
meta_rows = conn.execute(
|
||||
# Get all embedding IDs and their documents
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT em.key, em.string_value, em.int_value, em.float_value, em.bool_value
|
||||
FROM embedding_metadata em
|
||||
JOIN embeddings e ON e.id = em.id
|
||||
WHERE e.embedding_id = ?
|
||||
AND em.key NOT LIKE 'chroma:%'
|
||||
""",
|
||||
(embedding_id,),
|
||||
SELECT e.embedding_id,
|
||||
MAX(CASE WHEN em.key = 'chroma:document' THEN em.string_value END) as document
|
||||
FROM embeddings e
|
||||
JOIN embedding_metadata em ON em.id = e.id
|
||||
GROUP BY e.embedding_id
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
metadata = {}
|
||||
for mr in meta_rows:
|
||||
key = mr["key"]
|
||||
if mr["string_value"] is not None:
|
||||
metadata[key] = mr["string_value"]
|
||||
elif mr["int_value"] is not None:
|
||||
metadata[key] = mr["int_value"]
|
||||
elif mr["float_value"] is not None:
|
||||
metadata[key] = mr["float_value"]
|
||||
elif mr["bool_value"] is not None:
|
||||
metadata[key] = bool(mr["bool_value"])
|
||||
drawers = []
|
||||
for row in rows:
|
||||
embedding_id = row["embedding_id"]
|
||||
document = row["document"]
|
||||
if not document:
|
||||
continue
|
||||
|
||||
drawers.append(
|
||||
{
|
||||
"id": embedding_id,
|
||||
"document": document,
|
||||
"metadata": metadata,
|
||||
}
|
||||
)
|
||||
# Get metadata for this embedding
|
||||
meta_rows = conn.execute(
|
||||
"""
|
||||
SELECT em.key, em.string_value, em.int_value, em.float_value, em.bool_value
|
||||
FROM embedding_metadata em
|
||||
JOIN embeddings e ON e.id = em.id
|
||||
WHERE e.embedding_id = ?
|
||||
AND em.key NOT LIKE 'chroma:%'
|
||||
""",
|
||||
(embedding_id,),
|
||||
).fetchall()
|
||||
|
||||
metadata = {}
|
||||
for mr in meta_rows:
|
||||
key = mr["key"]
|
||||
if mr["string_value"] is not None:
|
||||
metadata[key] = mr["string_value"]
|
||||
elif mr["int_value"] is not None:
|
||||
metadata[key] = mr["int_value"]
|
||||
elif mr["float_value"] is not None:
|
||||
metadata[key] = mr["float_value"]
|
||||
elif mr["bool_value"] is not None:
|
||||
metadata[key] = bool(mr["bool_value"])
|
||||
|
||||
drawers.append(
|
||||
{
|
||||
"id": embedding_id,
|
||||
"document": document,
|
||||
"metadata": metadata,
|
||||
}
|
||||
)
|
||||
|
||||
conn.close()
|
||||
return drawers
|
||||
|
||||
|
||||
@@ -287,53 +293,63 @@ def migrate(palace_path: str, dry_run: bool = False, confirm: bool = False):
|
||||
print(f"\n Backing up to {backup_path}...")
|
||||
shutil.copytree(palace_path, backup_path)
|
||||
|
||||
# Build fresh palace in a temp directory (avoids chromadb reading old state)
|
||||
import tempfile
|
||||
|
||||
# Build fresh palace in a temp directory (avoids chromadb reading old state).
|
||||
# Wrap the whole import-and-swap dance in try/finally so the temp dir is
|
||||
# cleaned up if any of the chromadb writes, the verify count, or the
|
||||
# rename fails — without try/finally a crashed migration leaves a partial
|
||||
# palace dir under the system temp root that the user has to find by hand.
|
||||
temp_palace = tempfile.mkdtemp(prefix="mempalace_migrate_")
|
||||
print(f" Creating fresh palace in {temp_palace}...")
|
||||
fresh_backend = ChromaBackend()
|
||||
col = fresh_backend.get_or_create_collection(temp_palace, "mempalace_drawers")
|
||||
|
||||
# Re-import in batches
|
||||
batch_size = 500
|
||||
imported = 0
|
||||
for i in range(0, len(drawers), batch_size):
|
||||
batch = drawers[i : i + batch_size]
|
||||
col.add(
|
||||
ids=[d["id"] for d in batch],
|
||||
documents=[d["document"] for d in batch],
|
||||
metadatas=[d["metadata"] for d in batch],
|
||||
)
|
||||
imported += len(batch)
|
||||
print(f" Imported {imported}/{len(drawers)} drawers...")
|
||||
|
||||
# Verify before swapping
|
||||
final_count = col.count()
|
||||
del col
|
||||
del fresh_backend
|
||||
|
||||
# Swap: rename old palace aside, then move new one into place.
|
||||
# This avoids a window where both old and new are missing.
|
||||
print(" Swapping old palace for migrated version...")
|
||||
stale_path = palace_path + ".old"
|
||||
if os.path.exists(stale_path):
|
||||
shutil.rmtree(stale_path)
|
||||
os.replace(palace_path, stale_path)
|
||||
try:
|
||||
os.replace(temp_palace, palace_path)
|
||||
except OSError as e:
|
||||
# EXDEV = temp lives on a different filesystem; fall back to copy+delete.
|
||||
# Anything else is a real error — don't mask it with shutil.move.
|
||||
if getattr(e, "errno", None) != errno.EXDEV:
|
||||
_restore_stale_palace(palace_path, stale_path)
|
||||
raise
|
||||
print(f" Creating fresh palace in {temp_palace}...")
|
||||
fresh_backend = ChromaBackend()
|
||||
col = fresh_backend.get_or_create_collection(temp_palace, "mempalace_drawers")
|
||||
|
||||
# Re-import in batches
|
||||
batch_size = 500
|
||||
imported = 0
|
||||
for i in range(0, len(drawers), batch_size):
|
||||
batch = drawers[i : i + batch_size]
|
||||
col.add(
|
||||
ids=[d["id"] for d in batch],
|
||||
documents=[d["document"] for d in batch],
|
||||
metadatas=[d["metadata"] for d in batch],
|
||||
)
|
||||
imported += len(batch)
|
||||
print(f" Imported {imported}/{len(drawers)} drawers...")
|
||||
|
||||
# Verify before swapping
|
||||
final_count = col.count()
|
||||
del col
|
||||
del fresh_backend
|
||||
|
||||
# Swap: rename old palace aside, then move new one into place.
|
||||
# This avoids a window where both old and new are missing.
|
||||
print(" Swapping old palace for migrated version...")
|
||||
stale_path = palace_path + ".old"
|
||||
if os.path.exists(stale_path):
|
||||
shutil.rmtree(stale_path)
|
||||
os.replace(palace_path, stale_path)
|
||||
try:
|
||||
shutil.move(temp_palace, palace_path)
|
||||
except Exception:
|
||||
_restore_stale_palace(palace_path, stale_path)
|
||||
raise
|
||||
shutil.rmtree(stale_path, ignore_errors=True)
|
||||
os.replace(temp_palace, palace_path)
|
||||
except OSError as e:
|
||||
# EXDEV = temp lives on a different filesystem; fall back to copy+delete.
|
||||
# Anything else is a real error — don't mask it with shutil.move.
|
||||
if getattr(e, "errno", None) != errno.EXDEV:
|
||||
_restore_stale_palace(palace_path, stale_path)
|
||||
raise
|
||||
try:
|
||||
shutil.move(temp_palace, palace_path)
|
||||
except Exception:
|
||||
_restore_stale_palace(palace_path, stale_path)
|
||||
raise
|
||||
shutil.rmtree(stale_path, ignore_errors=True)
|
||||
finally:
|
||||
# On the happy path os.replace/shutil.move consumed temp_palace, so
|
||||
# the directory no longer exists at the temp location — the existence
|
||||
# guard makes this a no-op then. On any failure path it actually
|
||||
# removes the orphan.
|
||||
if os.path.exists(temp_palace):
|
||||
shutil.rmtree(temp_palace, ignore_errors=True)
|
||||
|
||||
print("\n Migration complete.")
|
||||
print(f" Drawers migrated: {final_count}")
|
||||
|
||||
+201
-22
@@ -33,6 +33,12 @@ from .palace import (
|
||||
upsert_closet_lines,
|
||||
)
|
||||
|
||||
# Module-level import so tests can patch it as
|
||||
# ``mempalace.miner.compute_hallways_for_wing``. The integration call
|
||||
# lives at the end of _mine_impl, alongside the existing
|
||||
# ``_compute_topic_tunnels_for_wing`` post-mine block.
|
||||
from .hallways import compute_hallways_for_wing
|
||||
|
||||
logger = logging.getLogger("mempalace_mcp")
|
||||
|
||||
READABLE_EXTENSIONS = {
|
||||
@@ -83,13 +89,20 @@ from .config import ( # noqa: E402 (kept here for the legacy alias)
|
||||
|
||||
DRAWER_UPSERT_BATCH_SIZE = 1000
|
||||
MAX_FILE_SIZE = 500 * 1024 * 1024 # 500 MB — skip files larger than this.
|
||||
# A single file producing more chunks than this is almost always a generated
|
||||
# artifact (CSV/JSON dump, lockfile not in SKIP_FILENAMES, etc.). Embedding
|
||||
# thousands of chunks from one file in one batch has triggered ONNX runtime
|
||||
# `bad allocation` errors on Windows (#1296). The cap is conservative: a
|
||||
# 500-chunk file at CHUNK_SIZE=800 is ~400 KB of source, which covers most
|
||||
# legitimate hand-written content while bounding the worst-case batch.
|
||||
MAX_CHUNKS_PER_FILE = 500
|
||||
# A safety rail against pathological generated artifacts (lockfiles not in
|
||||
# SKIP_FILENAMES, vendored data dumps, etc.). Originally 500 to bound ONNX
|
||||
# runtime `bad allocation` errors on Windows (#1296), but at CHUNK_SIZE=800
|
||||
# that capped legitimate long-form content (#1455: full-text scholarly
|
||||
# editions, novels) at ~400 KB. The new default leaves two orders of
|
||||
# magnitude of safety margin against the original lockfile case
|
||||
# (~1124 chunks for `pnpm-lock.yaml` per #1296) while not touching
|
||||
# hand-written prose. Per-ONNX-call exposure is bounded by
|
||||
# `DRAWER_UPSERT_BATCH_SIZE` (1000 chunks/batch) regardless of this cap,
|
||||
# so the cap is a per-file admission rail, not a per-batch limit. Lower
|
||||
# this via `MEMPALACE_MAX_CHUNKS_PER_FILE` or
|
||||
# `mempalace mine --max-chunks-per-file N` if you hit ONNX bad_alloc on
|
||||
# Windows; set to 0 to disable the cap entirely.
|
||||
MAX_CHUNKS_PER_FILE = 50_000
|
||||
# Long Claude Code sessions and large transcript exports routinely exceed
|
||||
# 10 MB. The cap exists as a defensive rail against pathological binary
|
||||
# files, not as a limit on legitimate text. Per-drawer size is bounded
|
||||
@@ -99,6 +112,48 @@ MAX_CHUNKS_PER_FILE = 500
|
||||
# memory before chunking), so memory use scales with source size too.
|
||||
|
||||
|
||||
def _resolve_max_chunks_per_file(override: Optional[int] = None) -> int:
|
||||
"""Resolve the effective per-file chunk cap.
|
||||
|
||||
Precedence: ``override`` (CLI flag) > ``MEMPALACE_MAX_CHUNKS_PER_FILE``
|
||||
env var > module-level ``MAX_CHUNKS_PER_FILE`` default. A sentinel
|
||||
value of ``0`` (from any source) disables the cap entirely. Negative
|
||||
values from either source emit a stderr warning and fall back to the
|
||||
module default so a misconfigured ``--max-chunks-per-file=-500`` typo
|
||||
(meaning "no, don't lower it that much") does not silently disable
|
||||
the cap and OOM on a generated artifact.
|
||||
"""
|
||||
if override is not None:
|
||||
if override < 0:
|
||||
print(
|
||||
f" ! WARNING: --max-chunks-per-file={override} is negative; "
|
||||
f"using default {MAX_CHUNKS_PER_FILE}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return MAX_CHUNKS_PER_FILE
|
||||
return int(override)
|
||||
raw = os.environ.get("MEMPALACE_MAX_CHUNKS_PER_FILE")
|
||||
if raw is None:
|
||||
return MAX_CHUNKS_PER_FILE
|
||||
try:
|
||||
val = int(raw)
|
||||
except ValueError:
|
||||
print(
|
||||
f" ! WARNING: MEMPALACE_MAX_CHUNKS_PER_FILE={raw!r} is not an integer; "
|
||||
f"using default {MAX_CHUNKS_PER_FILE}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return MAX_CHUNKS_PER_FILE
|
||||
if val < 0:
|
||||
print(
|
||||
f" ! WARNING: MEMPALACE_MAX_CHUNKS_PER_FILE={val} is negative; "
|
||||
f"using default {MAX_CHUNKS_PER_FILE}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return MAX_CHUNKS_PER_FILE
|
||||
return val
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# IGNORE MATCHING
|
||||
# =============================================================================
|
||||
@@ -785,7 +840,11 @@ def _extract_entities_for_metadata(content: str) -> str:
|
||||
|
||||
known = _load_known_entities()
|
||||
for name in known:
|
||||
if re.search(r"(?<!\w)" + re.escape(name) + r"(?!\w)", content):
|
||||
# Case-insensitive match — mirrors entity_detector.py's init-time
|
||||
# behavior so a known entity like "Aya" tags drawers that mention
|
||||
# "aya" / "AYA" / "Aya". Without re.IGNORECASE, lowercase mentions
|
||||
# in chat transcripts and voice-typed content get silently untagged.
|
||||
if re.search(r"(?<!\w)" + re.escape(name) + r"(?!\w)", content, re.IGNORECASE):
|
||||
matched.add(name)
|
||||
|
||||
from .palace import _candidate_entity_words
|
||||
@@ -883,23 +942,32 @@ def process_file(
|
||||
chunk_size: int = None,
|
||||
chunk_overlap: int = None,
|
||||
min_chunk_size: int = None,
|
||||
max_chunks_per_file: Optional[int] = None,
|
||||
) -> tuple:
|
||||
"""Read, chunk, route, and file one file. Returns (drawer_count, room_name)."""
|
||||
"""Read, chunk, route, and file one file.
|
||||
|
||||
Returns ``(drawer_count, room_name, skip_reason)``. ``skip_reason`` is
|
||||
``None`` on success and on every non-chunk-cap skip path: already
|
||||
filed (pre- or post-lock re-check), unreadable (``OSError``), or
|
||||
too-short content (below ``min_chunk_size``). It is ``"chunk_cap"``
|
||||
when the per-file chunk cap aborted the file. Callers use the tag to
|
||||
surface a separate counter in the mine summary (see #1455).
|
||||
"""
|
||||
effective_min = min_chunk_size if min_chunk_size is not None else MIN_CHUNK_SIZE
|
||||
|
||||
# Skip if already filed
|
||||
source_file = str(filepath)
|
||||
if not dry_run and file_already_mined(collection, source_file, check_mtime=True):
|
||||
return 0, "general"
|
||||
return 0, "general", None
|
||||
|
||||
try:
|
||||
content = filepath.read_text(encoding="utf-8", errors="replace")
|
||||
except OSError:
|
||||
return 0, "general"
|
||||
return 0, "general", None
|
||||
|
||||
content = content.strip()
|
||||
if len(content) < effective_min:
|
||||
return 0, "general"
|
||||
return 0, "general", None
|
||||
|
||||
room = detect_room(filepath, content, rooms, project_path)
|
||||
chunks = chunk_text(
|
||||
@@ -910,16 +978,24 @@ def process_file(
|
||||
min_chunk_size=min_chunk_size,
|
||||
)
|
||||
|
||||
if len(chunks) > MAX_CHUNKS_PER_FILE:
|
||||
effective_cap = _resolve_max_chunks_per_file(max_chunks_per_file)
|
||||
if effective_cap > 0 and len(chunks) > effective_cap:
|
||||
# Skip notice goes to stderr alongside the existing symlink-skip
|
||||
# warning style (see ``scan_project``'s ``SKIP: <rel> (symlink)``
|
||||
# line). This keeps ``mempalace mine ... > out.log 2> err.log``
|
||||
# piping coherent: degraded outcomes on stderr, progress on stdout.
|
||||
print(
|
||||
f" ! [skip] {filepath.name[:50]:50} produced {len(chunks)} chunks "
|
||||
f"(> {MAX_CHUNKS_PER_FILE}); add to SKIP_FILENAMES or .gitignore"
|
||||
f"(> {effective_cap}); raise via --max-chunks-per-file or "
|
||||
f"MEMPALACE_MAX_CHUNKS_PER_FILE (set 0 to disable), or add to "
|
||||
f"SKIP_FILENAMES if this is a generated artifact",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 0, room
|
||||
return 0, room, "chunk_cap"
|
||||
|
||||
if dry_run:
|
||||
print(f" [DRY RUN] {filepath.name} -> room:{room} ({len(chunks)} drawers)")
|
||||
return len(chunks), room
|
||||
return len(chunks), room, None
|
||||
|
||||
# Lock this file so concurrent agents don't interleave delete+insert.
|
||||
# Without the lock, two agents can both pass file_already_mined(),
|
||||
@@ -927,7 +1003,7 @@ def process_file(
|
||||
with mine_lock(source_file):
|
||||
# Re-check after acquiring lock — another agent may have just finished
|
||||
if file_already_mined(collection, source_file, check_mtime=True):
|
||||
return 0, room
|
||||
return 0, room, None
|
||||
|
||||
# Purge stale drawers for this file before re-inserting the fresh chunks.
|
||||
# Converts modified-file re-mines from upsert-over-existing-IDs (which hits
|
||||
@@ -1001,7 +1077,7 @@ def process_file(
|
||||
purge_file_closets(closets_col, source_file)
|
||||
upsert_closet_lines(closets_col, closet_id_base, closet_lines, closet_meta)
|
||||
|
||||
return drawers_added, room
|
||||
return drawers_added, room, None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -1098,6 +1174,7 @@ def mine(
|
||||
respect_gitignore: bool = True,
|
||||
include_ignored: list = None,
|
||||
files: list = None,
|
||||
max_chunks_per_file: Optional[int] = None,
|
||||
):
|
||||
"""Mine a project directory into the palace.
|
||||
|
||||
@@ -1106,6 +1183,11 @@ def mine(
|
||||
caller (e.g. ``init`` showing a file-count estimate before the mine
|
||||
prompt) avoids walking the tree twice. When ``None`` (the default),
|
||||
``mine`` walks the tree itself just like before.
|
||||
|
||||
``max_chunks_per_file`` overrides the per-file chunk cap (see
|
||||
:func:`_resolve_max_chunks_per_file`). ``None`` defers to
|
||||
``MEMPALACE_MAX_CHUNKS_PER_FILE`` or ``MAX_CHUNKS_PER_FILE``; ``0``
|
||||
disables the cap entirely (#1455).
|
||||
"""
|
||||
if dry_run:
|
||||
return _mine_impl(
|
||||
@@ -1118,6 +1200,7 @@ def mine(
|
||||
respect_gitignore=respect_gitignore,
|
||||
include_ignored=include_ignored,
|
||||
files=files,
|
||||
max_chunks_per_file=max_chunks_per_file,
|
||||
)
|
||||
|
||||
# MineAlreadyRunning propagates so the CLI can render a clear holder-aware
|
||||
@@ -1134,6 +1217,7 @@ def mine(
|
||||
respect_gitignore=respect_gitignore,
|
||||
include_ignored=include_ignored,
|
||||
files=files,
|
||||
max_chunks_per_file=max_chunks_per_file,
|
||||
)
|
||||
|
||||
|
||||
@@ -1147,6 +1231,7 @@ def _mine_impl(
|
||||
respect_gitignore: bool = True,
|
||||
include_ignored: list = None,
|
||||
files: list = None,
|
||||
max_chunks_per_file: Optional[int] = None,
|
||||
):
|
||||
from .config import MempalaceConfig
|
||||
|
||||
@@ -1197,14 +1282,16 @@ def _mine_impl(
|
||||
|
||||
total_drawers = 0
|
||||
files_skipped = 0
|
||||
files_skipped_chunk_cap = 0
|
||||
files_processed = 0
|
||||
last_file = None
|
||||
room_counts = defaultdict(int)
|
||||
effective_chunk_cap = _resolve_max_chunks_per_file(max_chunks_per_file)
|
||||
|
||||
try:
|
||||
for i, filepath in enumerate(files, 1):
|
||||
try:
|
||||
drawers, room = process_file(
|
||||
drawers, room, skip_reason = process_file(
|
||||
filepath=filepath,
|
||||
project_path=project_path,
|
||||
collection=collection,
|
||||
@@ -1216,6 +1303,11 @@ def _mine_impl(
|
||||
chunk_size=cfg_chunk_size,
|
||||
chunk_overlap=cfg_chunk_overlap,
|
||||
min_chunk_size=cfg_min_chunk_size,
|
||||
# Pass the already-resolved int so ``process_file``'s
|
||||
# ``override is not None`` branch skips the env re-read;
|
||||
# otherwise a malformed env var would emit its warning
|
||||
# per file.
|
||||
max_chunks_per_file=effective_chunk_cap,
|
||||
)
|
||||
except KeyboardInterrupt:
|
||||
# Re-raise so the outer handler prints the summary; we
|
||||
@@ -1224,8 +1316,15 @@ def _mine_impl(
|
||||
raise
|
||||
files_processed = i
|
||||
last_file = filepath.name
|
||||
if drawers == 0 and not dry_run:
|
||||
# All zero-drawer outcomes increment ``files_skipped`` in both
|
||||
# modes so the summary "Files processed" arithmetic and the
|
||||
# residual-skip counter stay honest under ``--dry-run`` too. The
|
||||
# chunk-cap counter is partitioned out for its dedicated
|
||||
# summary line (see #1455 + Gemini review on PR #1554).
|
||||
if drawers == 0:
|
||||
files_skipped += 1
|
||||
if skip_reason == "chunk_cap":
|
||||
files_skipped_chunk_cap += 1
|
||||
else:
|
||||
total_drawers += drawers
|
||||
room_counts[room] += 1
|
||||
@@ -1248,10 +1347,60 @@ def _mine_impl(
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
# Within-wing hallways: link entities (people, projects, concepts)
|
||||
# that co-occur in drawers across this wing's rooms. Mirrors the
|
||||
# tunnel-compute fault-tolerance pattern — hallway computation
|
||||
# must never fail a mine; it's a derived analytic, not load-bearing
|
||||
# for the drawer write that already committed above.
|
||||
try:
|
||||
hallways_created = compute_hallways_for_wing(wing, col=collection)
|
||||
if hallways_created:
|
||||
print(f"\n Hallways: +{len(hallways_created)} within-wing entity link(s)")
|
||||
except Exception as e:
|
||||
print(
|
||||
f"\n WARNING: hallway computation skipped — {e}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
# Cross-wing entity tunnels: derived from the hallway records
|
||||
# materialized just above. When an entity appears in hallways of
|
||||
# this wing AND another wing, a tunnel bridges them. Runs in
|
||||
# parallel with topic tunnels — both kinds coexist via
|
||||
# ``kind="entity"`` / ``kind="topic"``. Same fault-tolerance
|
||||
# pattern: never fail a mine over a derived analytic.
|
||||
try:
|
||||
entity_tunnels_added = _compute_entity_tunnels_for_wing(wing)
|
||||
if entity_tunnels_added:
|
||||
print(f"\n Entity tunnels: +{entity_tunnels_added} cross-wing entity link(s)")
|
||||
except Exception as e:
|
||||
print(
|
||||
f"\n WARNING: entity tunnel computation skipped — {e}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
print(f"\n{'=' * 55}")
|
||||
print(" Done.")
|
||||
print(f" Files processed: {len(files) - files_skipped}")
|
||||
print(f" Files skipped (already filed): {files_skipped}")
|
||||
# The residual skip bucket label depends on mode: dry-run bypasses
|
||||
# the already-mined check, so the only paths producing (0, room,
|
||||
# None) under dry_run are OSError / too-short / post-lock re-check
|
||||
# (and re-check itself is unreachable when nothing is being
|
||||
# written). Outside dry_run, the dominant case is "already filed".
|
||||
residual_label = (
|
||||
"Files skipped (read error or too short)"
|
||||
if dry_run
|
||||
else "Files skipped (already filed or other)"
|
||||
)
|
||||
print(f" {residual_label}: {max(0, files_skipped - files_skipped_chunk_cap)}")
|
||||
if files_skipped_chunk_cap > 0:
|
||||
# ``effective_chunk_cap`` is necessarily > 0 here: ``process_file``
|
||||
# only emits the ``"chunk_cap"`` skip_reason when its own
|
||||
# ``effective_cap > 0`` guard passes (see ``process_file``).
|
||||
print(
|
||||
f" Files skipped (chunk cap {effective_chunk_cap}): {files_skipped_chunk_cap} "
|
||||
f"(raise via --max-chunks-per-file or MEMPALACE_MAX_CHUNKS_PER_FILE; "
|
||||
f"set 0 to disable)"
|
||||
)
|
||||
print(f" Drawers filed: {total_drawers}")
|
||||
print("\n By room:")
|
||||
for room, count in sorted(room_counts.items(), key=lambda x: x[1], reverse=True):
|
||||
@@ -1323,7 +1472,12 @@ def _cleanup_mine_pid_file() -> None:
|
||||
if not pid_file.exists():
|
||||
return
|
||||
recorded = pid_file.read_text().strip()
|
||||
if recorded and recorded.isdigit() and int(recorded) == os.getpid():
|
||||
# PID file format: "{pid} {unix_timestamp}" (timestamp added in
|
||||
# #1552 for stale-by-age detection). Old-format files (bare
|
||||
# "{pid}") are also handled: split on whitespace and take the
|
||||
# first token as the PID.
|
||||
pid_token = recorded.split()[0] if recorded else ""
|
||||
if pid_token and pid_token.isdigit() and int(pid_token) == os.getpid():
|
||||
pid_file.unlink()
|
||||
except OSError:
|
||||
# Best-effort cleanup; never fail the mine over PID bookkeeping.
|
||||
@@ -1349,6 +1503,31 @@ def _compute_topic_tunnels_for_wing(wing: str) -> int:
|
||||
return len(created)
|
||||
|
||||
|
||||
def _compute_entity_tunnels_for_wing(wing: str) -> int:
|
||||
"""Drop tunnels between ``wing`` and every other wing that shares an
|
||||
entity via the within-wing hallway primitive.
|
||||
|
||||
Reads hallway records (``mempalace.hallways.list_hallways``) and
|
||||
materializes cross-wing tunnels for any entity that has hallways in
|
||||
this wing AND at least one other wing. Tunnels use ``kind="entity"``
|
||||
and the synthetic endpoint room ``entity:<name>`` so they're
|
||||
distinguishable from explicit and topic tunnels at read time but
|
||||
interchangeable with them via the standard ``list_tunnels`` /
|
||||
``follow_tunnels`` API.
|
||||
|
||||
Returns the number of tunnels created or refreshed. Zero means no
|
||||
eligible entity exists in this wing yet (or no hallway records do).
|
||||
"""
|
||||
from .hallways import list_hallways
|
||||
from .palace_graph import entity_tunnels_for_wing
|
||||
|
||||
hallways = list_hallways()
|
||||
if not hallways:
|
||||
return 0
|
||||
created = entity_tunnels_for_wing(wing, hallways)
|
||||
return len(created)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# STATUS
|
||||
# =============================================================================
|
||||
|
||||
+178
-23
@@ -335,30 +335,65 @@ def _fuzzy_match(query: str, nodes: dict, n: int = 5):
|
||||
# Explicit tunnels are created by agents when they notice a connection
|
||||
# between two specific drawers or rooms in different wings/projects.
|
||||
#
|
||||
# Stored as a JSON file at ~/.mempalace/tunnels.json so they persist
|
||||
# across palace rebuilds (not in ChromaDB which can be recreated).
|
||||
# Stored as a JSON file based on MempalaceConfig.palace_path (where the
|
||||
# palace itself lives) so they persist across palace rebuilds (not in
|
||||
# ChromaDB which can be recreated).
|
||||
|
||||
|
||||
_TUNNEL_FILE = os.path.join(os.path.expanduser("~"), ".mempalace", "tunnels.json")
|
||||
def _get_tunnel_file(config=None) -> str:
|
||||
"""Return the path to the tunnels.json file, derived from MempalaceConfig.palace_path."""
|
||||
config = config or MempalaceConfig()
|
||||
return config.tunnel_file
|
||||
|
||||
|
||||
def _load_tunnels():
|
||||
def _legacy_tunnel_file() -> str:
|
||||
"""The pre-3.3.6 hardcoded path. Kept only for one-time orphan detection."""
|
||||
return os.path.join(os.path.expanduser("~"), ".mempalace", "tunnels.json")
|
||||
|
||||
|
||||
def _load_tunnels(config=None):
|
||||
"""Load explicit tunnels from disk.
|
||||
|
||||
Returns an empty list if the file is missing or corrupt (e.g. truncated
|
||||
by a crash mid-write on a system that lacks atomic-rename semantics).
|
||||
|
||||
Backwards-compatibility: prior to 3.3.6 the tunnel file was hardcoded at
|
||||
``~/.mempalace/tunnels.json`` regardless of the configured palace_path.
|
||||
If the configured tunnel file is missing but a legacy file exists at a
|
||||
different path, log a one-line warning naming both paths so users can
|
||||
move the file manually. We do NOT auto-migrate — auto-merging tunnel
|
||||
state across two locations is too magical for a bugfix and risks
|
||||
clobbering newer data.
|
||||
|
||||
``config`` may be passed in by the caller to avoid re-instantiating
|
||||
``MempalaceConfig`` (which re-reads ``mempalace.yaml`` from disk) on
|
||||
every helper call within a single create_tunnel cycle.
|
||||
"""
|
||||
if not os.path.exists(_TUNNEL_FILE):
|
||||
return []
|
||||
try:
|
||||
with open(_TUNNEL_FILE, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
except Exception:
|
||||
return []
|
||||
return data if isinstance(data, list) else []
|
||||
current_tunnel_file = _get_tunnel_file(config)
|
||||
if os.path.exists(current_tunnel_file):
|
||||
try:
|
||||
with open(current_tunnel_file, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"Mempalace tunnels file '%s' is corrupt or unreadable; starting empty.",
|
||||
current_tunnel_file,
|
||||
)
|
||||
return []
|
||||
return data if isinstance(data, list) else []
|
||||
|
||||
legacy = _legacy_tunnel_file()
|
||||
if legacy != current_tunnel_file and os.path.exists(legacy):
|
||||
logger.warning(
|
||||
"Legacy tunnels file at '%s' is being ignored; configured location is '%s'. "
|
||||
"Move or copy the legacy file to the configured path to recover its tunnels.",
|
||||
legacy,
|
||||
current_tunnel_file,
|
||||
)
|
||||
return []
|
||||
|
||||
|
||||
def _save_tunnels(tunnels):
|
||||
def _save_tunnels(tunnels, config=None):
|
||||
"""Persist explicit tunnels atomically.
|
||||
|
||||
Writes to ``tunnels.json.tmp`` then ``os.replace``s it into place, so
|
||||
@@ -370,15 +405,19 @@ def _save_tunnels(tunnels):
|
||||
the user has explicitly linked) and should not be world-readable on
|
||||
shared Linux/multi-user systems. Matches the file-permission pattern
|
||||
established by #814 for the other sensitive palace files.
|
||||
|
||||
``config`` may be passed in by the caller to avoid re-instantiating
|
||||
``MempalaceConfig`` on every save.
|
||||
"""
|
||||
parent = os.path.dirname(_TUNNEL_FILE)
|
||||
tunnel_file = _get_tunnel_file(config)
|
||||
parent = os.path.dirname(tunnel_file)
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
try:
|
||||
os.chmod(parent, 0o700)
|
||||
except (OSError, NotImplementedError):
|
||||
# Windows / unsupported filesystems — tolerate.
|
||||
pass
|
||||
tmp_path = _TUNNEL_FILE + ".tmp"
|
||||
tmp_path = tunnel_file + ".tmp"
|
||||
with open(tmp_path, "w", encoding="utf-8") as f:
|
||||
json.dump(tunnels, f, indent=2)
|
||||
f.flush()
|
||||
@@ -387,9 +426,9 @@ def _save_tunnels(tunnels):
|
||||
except OSError:
|
||||
# Not all filesystems (or Windows file handles) support fsync — tolerate.
|
||||
pass
|
||||
os.replace(tmp_path, _TUNNEL_FILE)
|
||||
os.replace(tmp_path, tunnel_file)
|
||||
try:
|
||||
os.chmod(_TUNNEL_FILE, 0o600)
|
||||
os.chmod(tunnel_file, 0o600)
|
||||
except (OSError, NotImplementedError):
|
||||
pass
|
||||
|
||||
@@ -421,6 +460,30 @@ def _require_name(value: str, field: str) -> str:
|
||||
return value.strip()
|
||||
|
||||
|
||||
def _check_room_exists(wing: str, room: str, col) -> bool:
|
||||
"""Check if at least one drawer exists for the given wing/room in ChromaDB."""
|
||||
if col is None:
|
||||
# If collection is unreachable, can't verify, so allow.
|
||||
logger.debug(
|
||||
"ChromaDB collection not reachable, skipping room existence validation for %s/%s",
|
||||
wing,
|
||||
room,
|
||||
)
|
||||
return True
|
||||
try:
|
||||
results = col.get(where={"$and": [{"wing": wing}, {"room": room}]}, limit=1, include=[])
|
||||
return len(results["ids"]) > 0
|
||||
except Exception:
|
||||
# If query fails, assume it's a temporary issue or permissions, and allow.
|
||||
logger.warning(
|
||||
"Error checking room existence in ChromaDB for %s/%s; allowing tunnel creation.",
|
||||
wing,
|
||||
room,
|
||||
exc_info=True,
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def create_tunnel(
|
||||
source_wing: str,
|
||||
source_room: str,
|
||||
@@ -461,7 +524,8 @@ def create_tunnel(
|
||||
The stored tunnel dict.
|
||||
|
||||
Raises:
|
||||
ValueError: if any wing or room is empty or non-string.
|
||||
ValueError: if any wing or room is empty or non-string, or if an explicit
|
||||
tunnel points to a nonexistent room.
|
||||
|
||||
Note:
|
||||
Wing slugs are stored verbatim — passing ``"my-wing"`` and ``"my_wing"``
|
||||
@@ -475,6 +539,22 @@ def create_tunnel(
|
||||
target_wing = _require_name(target_wing, "target_wing")
|
||||
target_room = _require_name(target_room, "target_room")
|
||||
|
||||
# Single MempalaceConfig() per call — reused by _get_tunnel_file /
|
||||
# _load_tunnels / _save_tunnels below. Each MempalaceConfig() re-reads
|
||||
# mempalace.yaml from disk; before this change the helpers each
|
||||
# instantiated their own, triggering several redundant disk reads per
|
||||
# create_tunnel call (flagged by gemini-code-assist on #1469).
|
||||
config = MempalaceConfig()
|
||||
|
||||
# Validate room existence for explicit tunnels only. Use the verbatim wing
|
||||
# slugs here so #1504's hyphen-preserving write path remains intact.
|
||||
if kind == "explicit":
|
||||
col = _get_collection(config)
|
||||
if not _check_room_exists(source_wing, source_room, col):
|
||||
raise ValueError(f"Source room '{source_room}' does not exist in wing '{source_wing}'")
|
||||
if not _check_room_exists(target_wing, target_room, col):
|
||||
raise ValueError(f"Target room '{target_room}' does not exist in wing '{target_wing}'")
|
||||
|
||||
tunnel_id = _canonical_tunnel_id(source_wing, source_room, target_wing, target_room)
|
||||
|
||||
tunnel = {
|
||||
@@ -493,8 +573,8 @@ def create_tunnel(
|
||||
# Serialize the load → mutate → save cycle. Without this, two concurrent
|
||||
# create_tunnel calls can both read the same snapshot and the later
|
||||
# writer silently drops the earlier writer's tunnel.
|
||||
with mine_lock(_TUNNEL_FILE):
|
||||
tunnels = _load_tunnels()
|
||||
with mine_lock(_get_tunnel_file(config)):
|
||||
tunnels = _load_tunnels(config)
|
||||
for existing in tunnels:
|
||||
if existing.get("id") == tunnel_id:
|
||||
# Preserve original creation timestamp on label updates.
|
||||
@@ -502,10 +582,10 @@ def create_tunnel(
|
||||
tunnel["updated_at"] = datetime.now(timezone.utc).isoformat()
|
||||
existing.clear()
|
||||
existing.update(tunnel)
|
||||
_save_tunnels(tunnels)
|
||||
_save_tunnels(tunnels, config)
|
||||
return existing
|
||||
tunnels.append(tunnel)
|
||||
_save_tunnels(tunnels)
|
||||
_save_tunnels(tunnels, config)
|
||||
return tunnel
|
||||
|
||||
|
||||
@@ -535,7 +615,7 @@ def list_tunnels(wing: str = None):
|
||||
|
||||
def delete_tunnel(tunnel_id: str):
|
||||
"""Delete an explicit tunnel by ID. Returns ``{"deleted": <id>}``."""
|
||||
with mine_lock(_TUNNEL_FILE):
|
||||
with mine_lock(_get_tunnel_file()):
|
||||
tunnels = _load_tunnels()
|
||||
tunnels = [t for t in tunnels if t.get("id") != tunnel_id]
|
||||
_save_tunnels(tunnels)
|
||||
@@ -779,3 +859,78 @@ def topic_tunnels_for_wing(
|
||||
)
|
||||
)
|
||||
return created
|
||||
|
||||
|
||||
def entity_tunnels_for_wing(
|
||||
wing: str,
|
||||
hallways: list,
|
||||
label_prefix: str = "shared entity",
|
||||
) -> list:
|
||||
"""Compute entity tunnels involving a single wing.
|
||||
|
||||
An entity tunnel bridges two wings when the same entity (person,
|
||||
project, concept, interest) appears in within-wing hallways of both.
|
||||
This is the architectural counterpart to ``topic_tunnels_for_wing`` —
|
||||
same storage path (``create_tunnel`` → ``~/.mempalace/tunnels.json``),
|
||||
same dedup, same listing API — but the substrate is hallway records
|
||||
rather than raw topic words. See v4 architecture doc, Wing →
|
||||
Drawer-entities → Hallway → Tunnel.
|
||||
|
||||
Endpoints use the synthetic room id ``entity:<name>`` (mirrors
|
||||
``topic:<slug>``) so they can't collide with literal folder-derived
|
||||
rooms of the same name. Casing of the entity is preserved.
|
||||
|
||||
Topic tunnels are NOT replaced — both systems coexist for one release
|
||||
cycle while entity tunnels prove out. Deprecation is a separate PR.
|
||||
"""
|
||||
if not hallways or not isinstance(wing, str) or not wing.strip():
|
||||
return []
|
||||
|
||||
wing_norm = normalize_wing_name(wing.strip())
|
||||
|
||||
# Build: entity -> {normalized_wing -> original_wing_display_name}
|
||||
# Both entity_a and entity_b positions count toward "this entity is
|
||||
# in this wing"; the hallway primitive treats the pair as unordered.
|
||||
entity_wings: dict = {}
|
||||
for h in hallways:
|
||||
if not isinstance(h, dict):
|
||||
continue
|
||||
h_wing = h.get("wing")
|
||||
if not isinstance(h_wing, str) or not h_wing.strip():
|
||||
continue
|
||||
h_wing_norm = normalize_wing_name(h_wing.strip())
|
||||
for ent_key in ("entity_a", "entity_b"):
|
||||
ent = h.get(ent_key)
|
||||
if not isinstance(ent, str) or not ent.strip():
|
||||
continue
|
||||
# setdefault preserves the first-seen display form so the
|
||||
# tunnel endpoint matches the wing name the caller used.
|
||||
entity_wings.setdefault(ent, {}).setdefault(h_wing_norm, h_wing)
|
||||
|
||||
if not entity_wings:
|
||||
return []
|
||||
|
||||
created: list = []
|
||||
# Stable entity order so tunnels materialize deterministically across
|
||||
# runs — matters for tests and for diff-able tunnels.json files.
|
||||
for entity in sorted(entity_wings.keys()):
|
||||
wings_for_entity = entity_wings[entity]
|
||||
if wing_norm not in wings_for_entity:
|
||||
continue
|
||||
own_wing_display = wings_for_entity[wing_norm]
|
||||
# Stable other-wing order; ``wing_norm`` itself is excluded so an
|
||||
# entity that lives only in this wing produces zero tunnels.
|
||||
other_wings_norm = sorted(w for w in wings_for_entity if w != wing_norm)
|
||||
for other_norm in other_wings_norm:
|
||||
other_display = wings_for_entity[other_norm]
|
||||
room = f"entity:{entity}"
|
||||
tunnel = create_tunnel(
|
||||
source_wing=own_wing_display,
|
||||
source_room=room,
|
||||
target_wing=other_display,
|
||||
target_room=room,
|
||||
label=f"{label_prefix}: {entity}",
|
||||
kind="entity",
|
||||
)
|
||||
created.append(tunnel)
|
||||
return created
|
||||
|
||||
@@ -35,6 +35,7 @@ import shutil
|
||||
import sqlite3
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from contextlib import closing
|
||||
from datetime import datetime
|
||||
import re
|
||||
from typing import Callable, Iterator, Optional
|
||||
@@ -680,6 +681,40 @@ class _DefaultProgress:
|
||||
return f" (elapsed {_format_eta(elapsed)}, rate {rate:.1f}/s, ETA {_format_eta(eta)})"
|
||||
|
||||
|
||||
def _vacuum_and_rebuild_fts5(palace_path: str, progress=print) -> None:
|
||||
"""VACUUM the palace SQLite file and rebuild the FTS5 index if present.
|
||||
|
||||
Repeated ``repair --yes`` runs delete and recreate the drawers collection,
|
||||
leaving freed SQLite pages unreclaimable without an explicit VACUUM. The
|
||||
FTS5 virtual table (``embedding_fulltext_search``) can also become
|
||||
internally inconsistent after multiple collection deletes; the rebuild
|
||||
command fixes it atomically without touching any row data.
|
||||
|
||||
Failures are non-fatal: a warning is printed and the caller continues.
|
||||
The repair itself succeeded at this point — VACUUM/FTS5 are best-effort
|
||||
cleanup, not correctness requirements.
|
||||
"""
|
||||
sqlite_path = os.path.join(palace_path, "chroma.sqlite3")
|
||||
if not os.path.exists(sqlite_path):
|
||||
return
|
||||
try:
|
||||
with closing(sqlite3.connect(sqlite_path, isolation_level=None)) as conn:
|
||||
tables = {
|
||||
r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
||||
}
|
||||
if "embedding_fulltext_search" in tables:
|
||||
conn.execute(
|
||||
"INSERT INTO embedding_fulltext_search"
|
||||
"(embedding_fulltext_search) VALUES('rebuild')"
|
||||
)
|
||||
conn.commit()
|
||||
progress(" FTS5 index rebuilt.")
|
||||
conn.execute("VACUUM")
|
||||
progress(" SQLite VACUUM complete.")
|
||||
except Exception as exc:
|
||||
progress(f" Warning: post-repair cleanup failed (non-fatal): {exc}")
|
||||
|
||||
|
||||
def rebuild_index(
|
||||
palace_path=None,
|
||||
confirm_truncation_ok: bool = False,
|
||||
@@ -813,6 +848,9 @@ def rebuild_index(
|
||||
print(" Live collection was not replaced; leaving the original palace untouched.")
|
||||
raise
|
||||
|
||||
_close_chroma_handles(palace_path, backend=backend)
|
||||
_vacuum_and_rebuild_fts5(palace_path, progress=progress)
|
||||
|
||||
print(f"\n Repair complete. {filed} drawers rebuilt.")
|
||||
print(" HNSW index is now clean with cosine distance metric.")
|
||||
print(f"\n{'=' * 55}\n")
|
||||
|
||||
@@ -1017,3 +1017,61 @@ def search_memories(
|
||||
"total_before_filter": len(_first_or_empty(drawer_results, "documents")),
|
||||
"results": hits,
|
||||
}
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Virtual line numbering — read-time grid for drawers (3.3.6).
|
||||
#
|
||||
# Drawers are stored verbatim on disk. The reader applies a line-number grid
|
||||
# at read time so any drawer — numbered or not — can be sectioned by a closet
|
||||
# pointer like ``→2026-01-18:L55-L72`` without rewriting the corpus. Pure
|
||||
# functions, no I/O. Source drawer text is never mutated.
|
||||
# See docs/virtual-line-numbering.md for the full design rationale.
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
# A line is "already numbered" iff it starts with [<digits>].
|
||||
_ALREADY_NUMBERED_RE = re.compile(r"^\[\d+\]")
|
||||
|
||||
|
||||
def render_with_line_numbers(text: "str | None", start_line: int = 1) -> str:
|
||||
"""Prefix each line of ``text`` with ``[N] `` for read-time grid display.
|
||||
|
||||
Lines that already begin with ``[<digits>]`` pass through unchanged,
|
||||
but the counter still advances on them so callers can rely on positional
|
||||
alignment with the original line indices.
|
||||
|
||||
``None`` is treated as empty string. Pure function.
|
||||
"""
|
||||
if not text:
|
||||
return ""
|
||||
out = []
|
||||
for i, line in enumerate(text.split("\n"), start=start_line):
|
||||
if _ALREADY_NUMBERED_RE.match(line):
|
||||
out.append(line)
|
||||
else:
|
||||
out.append(f"[{i}] {line}")
|
||||
return "\n".join(out)
|
||||
|
||||
|
||||
def extract_line_range(text: str, line_start: int, line_end: int) -> str:
|
||||
"""Return the 1-indexed inclusive slice ``[line_start, line_end]`` rendered with line numbers.
|
||||
|
||||
This is the closet-pointer read path. A pointer like ``→2026-01-18:L55-L72``
|
||||
resolves by opening the day-drawer and calling ``extract_line_range(drawer_text, 55, 72)``.
|
||||
Out-of-bounds ranges are clamped. Invalid ranges return ``""``.
|
||||
"""
|
||||
if not text:
|
||||
return ""
|
||||
if line_end < line_start:
|
||||
return ""
|
||||
|
||||
lines = text.split("\n")
|
||||
effective_start = max(1, line_start)
|
||||
effective_end = min(len(lines), line_end)
|
||||
|
||||
if effective_start > effective_end:
|
||||
return ""
|
||||
|
||||
section = "\n".join(lines[effective_start - 1 : effective_end])
|
||||
return render_with_line_numbers(section, start_line=effective_start)
|
||||
|
||||
+17
-2
@@ -51,7 +51,7 @@ chroma = "mempalace.backends.chroma:ChromaBackend"
|
||||
[project.entry-points."mempalace.sources"]
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = ["pytest>=7.0", "pytest-cov>=4.0", "ruff>=0.4.0", "psutil>=5.9"]
|
||||
dev = ["pytest>=7.0", "pytest-cov>=4.0", "ruff==0.15.9", "psutil>=5.9"]
|
||||
spellcheck = ["autocorrect>=2.0"]
|
||||
# Hardware acceleration for the ONNX embedding model. Install exactly one:
|
||||
# pip install mempalace[gpu] — NVIDIA CUDA
|
||||
@@ -61,9 +61,24 @@ spellcheck = ["autocorrect>=2.0"]
|
||||
gpu = ["onnxruntime-gpu>=1.16"]
|
||||
dml = ["onnxruntime-directml>=1.16"]
|
||||
coreml = ["onnxruntime>=1.16"]
|
||||
# Binary-format extraction for `mempalace mine --mode extract`. Per-format
|
||||
# transformer routing: striprtf for .rtf, MarkItDown for .pdf/.docx/.pptx/
|
||||
# .xlsx/.epub. MarkItDown requires Python ≥ 3.10 (env marker ensures it
|
||||
# only installs where supported; Python 3.9 users still get RTF coverage).
|
||||
extract = [
|
||||
"striprtf>=0.0.27",
|
||||
# Per PR #1555 review (Igor): bare ``markitdown`` is enough to import
|
||||
# but not enough to convert — actual PDF/DOCX/PPTX/XLSX extraction
|
||||
# needs MarkItDown's per-format sub-extras, which pull pdfminer-six,
|
||||
# mammoth, python-pptx, openpyxl, etc. EPUB is handled by base
|
||||
# markitdown (EpubConverter uses beautifulsoup4, a base requirement),
|
||||
# so no [epub] sub-extra is needed (and none exists in 0.1.5). RTF is
|
||||
# covered by striprtf above.
|
||||
"markitdown[docx,pdf,pptx,xlsx]>=0.1.5; python_version >= '3.10'",
|
||||
]
|
||||
|
||||
[dependency-groups]
|
||||
dev = ["pytest>=7.0", "pytest-cov>=4.0", "ruff>=0.4.0", "psutil>=5.9"]
|
||||
dev = ["pytest>=7.0", "pytest-cov>=4.0", "ruff==0.15.9", "psutil>=5.9"]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
|
||||
+59
-6
@@ -823,9 +823,9 @@ def test_make_client_quarantines_only_on_first_call_per_palace(tmp_path, monkeyp
|
||||
ChromaBackend.make_client(palace_path)
|
||||
ChromaBackend.make_client(palace_path)
|
||||
|
||||
assert calls == [
|
||||
palace_path
|
||||
], "quarantine_stale_hnsw should fire once per palace per process, not on every reconnect"
|
||||
assert calls == [palace_path], (
|
||||
"quarantine_stale_hnsw should fire once per palace per process, not on every reconnect"
|
||||
)
|
||||
|
||||
|
||||
def test_make_client_gates_invalid_metadata_on_first_call(tmp_path, monkeypatch):
|
||||
@@ -941,9 +941,9 @@ def test_client_quarantines_only_on_first_call_per_palace(tmp_path, monkeypatch)
|
||||
finally:
|
||||
backend.close()
|
||||
|
||||
assert (
|
||||
calls == [palace_path]
|
||||
), "quarantine_stale_hnsw should fire once per palace per process from _client(), not on every call"
|
||||
assert calls == [palace_path], (
|
||||
"quarantine_stale_hnsw should fire once per palace per process from _client(), not on every call"
|
||||
)
|
||||
|
||||
|
||||
# ── _pin_hnsw_threads (per-process retrofit, separate from this PR's gate) ──
|
||||
@@ -1047,6 +1047,59 @@ def test_quarantine_invalid_hnsw_metadata_renames_missing_dimensionality(tmp_pat
|
||||
assert not seg.exists()
|
||||
|
||||
|
||||
def test_quarantine_invalid_hnsw_metadata_keeps_consistent_missing_dimensionality(tmp_path):
|
||||
palace = tmp_path / "palace"
|
||||
palace.mkdir()
|
||||
seg = palace / "abcd-1234-5678"
|
||||
seg.mkdir()
|
||||
(seg / "data_level0.bin").write_bytes(b"x" * 2048)
|
||||
(seg / "link_lists.bin").write_bytes(b"x" * 128)
|
||||
with open(seg / "index_metadata.pickle", "wb") as f:
|
||||
pickle.dump(
|
||||
{
|
||||
"dimensionality": None,
|
||||
"total_elements_added": 2,
|
||||
"max_seq_id": None,
|
||||
"id_to_label": {"a": 1, "b": 2},
|
||||
"label_to_id": {1: "a", 2: "b"},
|
||||
"id_to_seq_id": {},
|
||||
},
|
||||
f,
|
||||
)
|
||||
|
||||
moved = quarantine_invalid_hnsw_metadata(str(palace))
|
||||
|
||||
assert moved == []
|
||||
assert seg.exists()
|
||||
|
||||
|
||||
def test_quarantine_invalid_hnsw_metadata_renames_mismatched_missing_dimensionality(tmp_path):
|
||||
palace = tmp_path / "palace"
|
||||
palace.mkdir()
|
||||
seg = palace / "abcd-1234-5678"
|
||||
seg.mkdir()
|
||||
(seg / "data_level0.bin").write_bytes(b"x" * 2048)
|
||||
(seg / "link_lists.bin").write_bytes(b"x" * 128)
|
||||
with open(seg / "index_metadata.pickle", "wb") as f:
|
||||
pickle.dump(
|
||||
{
|
||||
"dimensionality": None,
|
||||
"total_elements_added": 2,
|
||||
"max_seq_id": None,
|
||||
"id_to_label": {"a": 1, "b": 2},
|
||||
"label_to_id": {1: "b", 2: "a"},
|
||||
"id_to_seq_id": {},
|
||||
},
|
||||
f,
|
||||
)
|
||||
|
||||
moved = quarantine_invalid_hnsw_metadata(str(palace))
|
||||
|
||||
assert len(moved) == 1
|
||||
assert ".corrupt-" in moved[0]
|
||||
assert not seg.exists()
|
||||
|
||||
|
||||
def test_quarantine_invalid_hnsw_metadata_allows_uninitialized_segment(tmp_path):
|
||||
palace = tmp_path / "palace"
|
||||
palace.mkdir()
|
||||
|
||||
@@ -317,9 +317,9 @@ def test_read_path_does_not_acquire_lock(tmp_path, monkeypatch):
|
||||
if method is None:
|
||||
continue
|
||||
src = inspect.getsource(method)
|
||||
assert (
|
||||
"_write_lock" not in src
|
||||
), f"{read_attr} must NOT acquire the write lock (read path)"
|
||||
assert "_write_lock" not in src, (
|
||||
f"{read_attr} must NOT acquire the write lock (read path)"
|
||||
)
|
||||
finally:
|
||||
open(release, "w").close()
|
||||
holder.join(timeout=5)
|
||||
|
||||
@@ -51,23 +51,23 @@ def test_plugin_hook_timeout_within_bounds(hook_config: dict, event: str) -> Non
|
||||
)
|
||||
for entry in entries:
|
||||
sub_hooks = entry.get("hooks")
|
||||
assert (
|
||||
isinstance(sub_hooks, list) and sub_hooks
|
||||
), f"{event} entry missing non-empty 'hooks' array"
|
||||
assert (
|
||||
len(sub_hooks) == 1
|
||||
), f"{event} entry expected exactly one hook command, found {len(sub_hooks)}"
|
||||
assert isinstance(sub_hooks, list) and sub_hooks, (
|
||||
f"{event} entry missing non-empty 'hooks' array"
|
||||
)
|
||||
assert len(sub_hooks) == 1, (
|
||||
f"{event} entry expected exactly one hook command, found {len(sub_hooks)}"
|
||||
)
|
||||
for hook in sub_hooks:
|
||||
assert (
|
||||
hook.get("type") == "command"
|
||||
), f"unexpected hook type for {event}: {hook.get('type')!r}"
|
||||
assert hook.get("type") == "command", (
|
||||
f"unexpected hook type for {event}: {hook.get('type')!r}"
|
||||
)
|
||||
assert "timeout" in hook, f"{event} hook missing 'timeout' key"
|
||||
timeout = hook["timeout"]
|
||||
# bool subclasses int, so reject it explicitly: True == 1 must fail.
|
||||
is_real_int = isinstance(timeout, int) and not isinstance(timeout, bool)
|
||||
assert (
|
||||
is_real_int and floor <= timeout <= ceiling
|
||||
), f"{event} hook timeout must be an int in [{floor}, {ceiling}]s; got {timeout!r}"
|
||||
assert is_real_int and floor <= timeout <= ceiling, (
|
||||
f"{event} hook timeout must be an int in [{floor}, {ceiling}]s; got {timeout!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_no_unbounded_events_in_plugin_config(hook_config: dict) -> None:
|
||||
|
||||
+7
-6
@@ -79,12 +79,12 @@ def test_cli_main_strips_leaked_pythonpath_from_env():
|
||||
)
|
||||
diag = f"rc={result.returncode}; stdout={result.stdout!r}; stderr={result.stderr!r}"
|
||||
assert result.returncode == 0, f"subprocess failed: {diag}"
|
||||
assert (
|
||||
f"ENV_MID: {expected_env!r}" in result.stdout
|
||||
), f"package import unexpectedly stripped env (regression in __init__.py): {diag}"
|
||||
assert (
|
||||
"SENTINEL_IN_PATH: False" in result.stdout
|
||||
), f"package import did not filter sys.path (regression in __init__.py): {diag}"
|
||||
assert f"ENV_MID: {expected_env!r}" in result.stdout, (
|
||||
f"package import unexpectedly stripped env (regression in __init__.py): {diag}"
|
||||
)
|
||||
assert "SENTINEL_IN_PATH: False" in result.stdout, (
|
||||
f"package import did not filter sys.path (regression in __init__.py): {diag}"
|
||||
)
|
||||
assert "ENV_AFTER: None" in result.stdout, f"CLI did not strip PYTHONPATH: {diag}"
|
||||
|
||||
|
||||
@@ -566,6 +566,7 @@ def test_cmd_mine_projects_mode(mock_config_cls):
|
||||
dry_run=False,
|
||||
respect_gitignore=True,
|
||||
include_ignored=[],
|
||||
max_chunks_per_file=None,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -312,9 +312,9 @@ class TestRegenerateClosets:
|
||||
survivors = closets.get(where={"source_file": source}, include=["documents", "metadatas"])
|
||||
assert survivors["ids"], "LLM closets should have been written"
|
||||
joined = "\n".join(survivors["documents"])
|
||||
assert (
|
||||
"STALE_REGEX_TOPIC" not in joined
|
||||
), "pre-existing regex closet was not purged before LLM write"
|
||||
assert "STALE_REGEX_TOPIC" not in joined, (
|
||||
"pre-existing regex closet was not purged before LLM write"
|
||||
)
|
||||
assert "jwt auth" in joined
|
||||
for meta in survivors["metadatas"]:
|
||||
assert meta.get("generated_by", "").startswith("llm:")
|
||||
@@ -399,9 +399,9 @@ class TestRegenerateClosets:
|
||||
# Three paginated calls: (limit=5000, offset=0), (5000, 5000), (5000, 10000).
|
||||
assert len(get_calls) == 3, f"expected 3 batched fetches, got {len(get_calls)}"
|
||||
for call in get_calls:
|
||||
assert (
|
||||
call["limit"] == 5000
|
||||
), f"batch must be 5000 — got {call['limit']} (would risk SQLITE_MAX_VARIABLE_NUMBER)"
|
||||
assert call["limit"] == 5000, (
|
||||
f"batch must be 5000 — got {call['limit']} (would risk SQLITE_MAX_VARIABLE_NUMBER)"
|
||||
)
|
||||
# include must still request both documents and metadatas
|
||||
assert "documents" in call["include"]
|
||||
assert "metadatas" in call["include"]
|
||||
|
||||
+318
-27
@@ -31,6 +31,7 @@ import tempfile
|
||||
import threading
|
||||
import time
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from mempalace.miner import (
|
||||
@@ -140,9 +141,9 @@ class TestMineLock:
|
||||
# Sort by entry time and verify the second entry is after the first exit.
|
||||
intervals.sort(key=lambda iv: iv[1])
|
||||
(_, enter_a, exit_a), (_, enter_b, exit_b) = intervals
|
||||
assert (
|
||||
enter_a < exit_a <= enter_b < exit_b
|
||||
), f"critical sections overlapped — lock failed to serialize: {intervals}"
|
||||
assert enter_a < exit_a <= enter_b < exit_b, (
|
||||
f"critical sections overlapped — lock failed to serialize: {intervals}"
|
||||
)
|
||||
|
||||
|
||||
# ── build_closet_lines ─────────────────────────────────────────────────
|
||||
@@ -315,15 +316,15 @@ class TestMinerClosetRebuild:
|
||||
second_docs = "\n".join(second_pass["documents"]).lower()
|
||||
assert "only topic now" in second_docs
|
||||
for i in range(15):
|
||||
assert (
|
||||
f"topic {i}\n" not in second_docs
|
||||
), f"stale 'Topic {i}' from first mine survived the rebuild"
|
||||
assert f"topic {i}\n" not in second_docs, (
|
||||
f"stale 'Topic {i}' from first mine survived the rebuild"
|
||||
)
|
||||
# Numbered closets that existed only in the larger first run must be gone.
|
||||
leftover = first_ids - set(second_pass["ids"])
|
||||
for stale_id in leftover:
|
||||
assert not col.get(ids=[stale_id])[
|
||||
"ids"
|
||||
], f"orphan closet {stale_id} from larger first run survived purge"
|
||||
assert not col.get(ids=[stale_id])["ids"], (
|
||||
f"orphan closet {stale_id} from larger first run survived purge"
|
||||
)
|
||||
|
||||
|
||||
# ── _extract_drawer_ids_from_closet ───────────────────────────────────
|
||||
@@ -702,9 +703,9 @@ class TestDiaryIngest:
|
||||
|
||||
# No state file inside the user's diary dir.
|
||||
for entry in diary_dir.iterdir():
|
||||
assert (
|
||||
"diary_ingest" not in entry.name
|
||||
), f"state file leaked into user diary dir: {entry}"
|
||||
assert "diary_ingest" not in entry.name, (
|
||||
f"state file leaked into user diary dir: {entry}"
|
||||
)
|
||||
|
||||
# State file does exist under ~/.mempalace/state/.
|
||||
state_path = _state_file_for(str(palace_dir), diary_dir.resolve())
|
||||
@@ -732,14 +733,17 @@ class TestDiaryIngest:
|
||||
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import _diary_drawer_id, ingest_diaries
|
||||
from mempalace.diary_ingest import _diary_drawer_id_entry, ingest_diaries
|
||||
|
||||
ingest_diaries(str(personal_dir), str(palace_dir), wing="personal", force=True)
|
||||
ingest_diaries(str(work_dir), str(palace_dir), wing="work", force=True)
|
||||
|
||||
col = get_collection(str(palace_dir))
|
||||
personal_id = _diary_drawer_id("personal", "2026-04-13")
|
||||
work_id = _diary_drawer_id("work", "2026-04-13")
|
||||
# Post-#1539: per-entry drawers. Each single-entry diary produces
|
||||
# one drawer keyed by (wing, date, entry_idx=0, chunk_idx=0). The
|
||||
# wing component still keeps work vs personal collisions apart.
|
||||
personal_id = _diary_drawer_id_entry("personal", "2026-04-13", 0, 0)
|
||||
work_id = _diary_drawer_id_entry("work", "2026-04-13", 0, 0)
|
||||
assert personal_id != work_id
|
||||
|
||||
personal = col.get(ids=[personal_id])
|
||||
@@ -749,27 +753,314 @@ class TestDiaryIngest:
|
||||
assert "Personal-only marker." in personal["documents"][0]
|
||||
assert "Work-only marker." in work["documents"][0]
|
||||
|
||||
# ── #1539: per-entry drawers, oversized-entry chunking ─────────
|
||||
|
||||
def test_diary_multiple_entries_one_drawer_each(self, tmp_path):
|
||||
"""Regression for #1539: each ``##`` entry must become its own
|
||||
drawer rather than the whole file being one document. Pre-fix
|
||||
behaviour: a single file-level drawer for any number of entries.
|
||||
Post-fix: one drawer per entry."""
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
(diary_dir / "2026-04-13.md").write_text(
|
||||
"# 2026-04-13\n\n"
|
||||
"## 10:00 — first\n\nfirst body content here, well above min size.\n\n"
|
||||
"## 11:00 — second\n\nsecond body content with enough length here.\n\n"
|
||||
"## 12:00 — third\n\nthird body content with sufficient text in it.\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import ingest_diaries
|
||||
|
||||
ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
count = get_collection(str(palace_dir)).count()
|
||||
assert count == 3, f"expected one drawer per ## entry (3 entries); got {count} drawers"
|
||||
|
||||
def test_large_diary_with_oversized_entry_chunks_within_entry(self, tmp_path):
|
||||
"""Regression for #1539: when a single ``##`` entry exceeds
|
||||
chunk_size, the ingester must chunk that entry into bounded
|
||||
drawers rather than uploading the whole entry as one oversized
|
||||
document (which crashes the embedding model in production)."""
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
# Middle entry body is 1500 chars — above CHUNK_SIZE=800 but below
|
||||
# the embedding model's hard token limit. This trips the size-cap
|
||||
# assertion on develop without crashing the test infrastructure.
|
||||
big_body = "X" * 1500
|
||||
(diary_dir / "2026-04-13.md").write_text(
|
||||
f"# 2026-04-13\n\n"
|
||||
f"## 10:00 — first\n\nfirst body content here, well above min size.\n\n"
|
||||
f"## 11:00 — big\n\n{big_body}\n\n"
|
||||
f"## 12:00 — last\n\nlast body content with sufficient text in it.\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import ingest_diaries
|
||||
|
||||
ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
col = get_collection(str(palace_dir))
|
||||
drawers = col.get()
|
||||
docs = drawers["documents"]
|
||||
|
||||
max_len = max(len(d) for d in docs)
|
||||
assert max_len <= 800, (
|
||||
f"no drawer document may exceed CHUNK_SIZE=800; got max_len={max_len}"
|
||||
)
|
||||
# 3 entries with the middle entry split into >= 2 chunks → >= 4 drawers
|
||||
assert len(docs) >= 4, (
|
||||
f"oversized middle entry must produce multiple drawers; got {len(docs)} total drawers"
|
||||
)
|
||||
|
||||
def test_incremental_appends_new_entry_only(self, tmp_path):
|
||||
"""Regression for #1539: incremental ingest must add exactly the
|
||||
delta when one new entry is appended (not re-rewrite all)."""
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
diary_file = diary_dir / "2026-04-13.md"
|
||||
diary_file.write_text(
|
||||
"# 2026-04-13\n\n"
|
||||
"## 10:00 — first\n\nfirst body content, sufficiently long.\n\n"
|
||||
"## 11:00 — second\n\nsecond body content with text in it.\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import ingest_diaries
|
||||
|
||||
ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
initial = get_collection(str(palace_dir)).count()
|
||||
assert initial == 2, f"baseline: 2 entries → 2 drawers; got {initial}"
|
||||
|
||||
diary_file.write_text(
|
||||
diary_file.read_text()
|
||||
+ "\n## 12:00 — third\n\nthird body, newly added on second run.\n"
|
||||
)
|
||||
ingest_diaries(str(diary_dir), str(palace_dir))
|
||||
final = get_collection(str(palace_dir)).count()
|
||||
assert final == 3, f"after appending 1 entry: 3 drawers total; got {final}"
|
||||
|
||||
def test_entry_count_watermark_matches_drawer_count(self, tmp_path):
|
||||
"""Regression for #1539: persisted ``entry_count`` watermark
|
||||
must equal the number of entries split from the file (and
|
||||
therefore the number of drawers under the new schema)."""
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
(diary_dir / "2026-04-13.md").write_text(
|
||||
"# 2026-04-13\n\n"
|
||||
"## 10:00 — a\n\nbody one with enough text to count.\n\n"
|
||||
"## 11:00 — b\n\nbody two with enough text to count.\n\n"
|
||||
"## 12:00 — c\n\nbody three with enough text to count.\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import _state_file_for, _split_entries, ingest_diaries
|
||||
|
||||
ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
|
||||
state = json.loads(_state_file_for(str(palace_dir), diary_dir.resolve()).read_text())
|
||||
key = "diary|2026-04-13.md"
|
||||
assert state[key]["entry_count"] == 3, (
|
||||
f"watermark must equal entry count; got {state[key]['entry_count']}"
|
||||
)
|
||||
|
||||
text = (diary_dir / "2026-04-13.md").read_text()
|
||||
assert len(_split_entries(text)) == state[key]["entry_count"]
|
||||
|
||||
def test_diary_chunk_index_is_global_across_entries(self, tmp_path):
|
||||
"""Regression for #1539 review: ``chunk_index`` must be a global
|
||||
counter across the file (not per-entry) so the searcher's
|
||||
``_expand_with_neighbors`` (which queries by source_file +
|
||||
chunk_index range) can stitch sibling chunks regardless of
|
||||
entry boundary. Pre-fix the metadata key was ``entry_chunk_index``
|
||||
and search neighbor expansion silently fell back to the matched
|
||||
drawer alone for any diary hit."""
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
# 3 entries; middle is oversized (1500 chars > CHUNK_SIZE=800).
|
||||
big_body = "X" * 1500
|
||||
(diary_dir / "2026-04-13.md").write_text(
|
||||
f"# 2026-04-13\n\n"
|
||||
f"## 10:00 — a\n\nfirst body content with enough length here.\n\n"
|
||||
f"## 11:00 — b\n\n{big_body}\n\n"
|
||||
f"## 12:00 — c\n\nthird body content with enough length here.\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import ingest_diaries
|
||||
|
||||
ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
col = get_collection(str(palace_dir))
|
||||
all_drawers = col.get()
|
||||
|
||||
indices = sorted(m["chunk_index"] for m in all_drawers["metadatas"])
|
||||
# Global counter: 0, 1, 2, ... contiguous across all entries.
|
||||
assert indices == list(range(len(indices))), (
|
||||
f"chunk_index must be contiguous 0..N-1 globally; got {indices}"
|
||||
)
|
||||
# All chunks have the same source_file (filter target for neighbor
|
||||
# expansion). Verify the searcher-required pair (source_file +
|
||||
# chunk_index) is consistent on every drawer.
|
||||
sources = {m["source_file"] for m in all_drawers["metadatas"]}
|
||||
assert len(sources) == 1, f"all drawers must share one source_file; got {sources}"
|
||||
|
||||
def test_diary_entry_deletion_purges_orphan_drawers(self, tmp_path):
|
||||
"""Regression for #1539 review: if a diary shrinks (entries
|
||||
deleted), the full-rebuild step must purge prior-pass drawers
|
||||
for that ``source_file`` before re-writing — otherwise trailing
|
||||
drawers from the longer prior pass remain as orphans and
|
||||
pollute search results forever."""
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
diary_file = diary_dir / "2026-04-13.md"
|
||||
diary_file.write_text(
|
||||
"# 2026-04-13\n\n"
|
||||
"## 10:00 — first\n\nfirst body content with enough length here.\n\n"
|
||||
"## 11:00 — second\n\nsecond body content with enough length here.\n\n"
|
||||
"## 12:00 — third\n\nthird body content with enough length here.\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import ingest_diaries
|
||||
|
||||
ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
col = get_collection(str(palace_dir))
|
||||
assert col.count() == 3, f"baseline: 3 entries → 3 drawers; got {col.count()}"
|
||||
|
||||
# Shrink the file to 1 entry. Hash changes → content_changed=True
|
||||
# → full_rebuild=True → purge step must remove the trailing 2 drawers.
|
||||
diary_file.write_text(
|
||||
"# 2026-04-13\n\n## 10:00 — first\n\nfirst body content with enough length here.\n"
|
||||
)
|
||||
ingest_diaries(str(diary_dir), str(palace_dir))
|
||||
final = col.count()
|
||||
assert final == 1, (
|
||||
f"after shrinking 3 entries → 1 entry: exactly 1 drawer; "
|
||||
f"got {final} (orphans from prior pass not purged)"
|
||||
)
|
||||
|
||||
def test_diary_header_only_entry_produces_drawer(self, tmp_path):
|
||||
"""A ``##`` header with no body still produces a drawer carrying
|
||||
the header text. Catches a regression where ``if body`` would
|
||||
collapse the entry."""
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
(diary_dir / "2026-04-13.md").write_text(
|
||||
"# 2026-04-13\n\n"
|
||||
"## 10:00 — note one with sufficient length to pass min size\n\n"
|
||||
"## 11:00 — note two with sufficient length to pass min size\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
from mempalace.diary_ingest import ingest_diaries
|
||||
|
||||
ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
col = get_collection(str(palace_dir))
|
||||
assert col.count() == 2, (
|
||||
f"expected 2 drawers (one per header-only entry); got {col.count()}"
|
||||
)
|
||||
docs = col.get()["documents"]
|
||||
# Each drawer holds the header line itself.
|
||||
joined = "\n".join(docs)
|
||||
assert "note one" in joined
|
||||
assert "note two" in joined
|
||||
|
||||
def test_diary_atomic_batched_upsert_on_failure(self, tmp_path, monkeypatch):
|
||||
"""A mid-pass embedding failure must leave the collection empty
|
||||
rather than half-written. Without batched-atomic upsert, an
|
||||
earlier per-loop upsert would partially commit and the state
|
||||
file would skip the file on re-run, silently losing entries.
|
||||
|
||||
The fixture mixes normal entries with one oversized entry so
|
||||
the failed batch exercises both the single-entry-per-drawer
|
||||
branch and the per-entry character-chunk fallback."""
|
||||
from mempalace import diary_ingest
|
||||
|
||||
diary_dir = tmp_path / "diaries"
|
||||
diary_dir.mkdir()
|
||||
(diary_dir / "2026-05-18.md").write_text(
|
||||
"# 2026-05-18\n\n"
|
||||
"## 10:00 — entry one with sufficient body to clear min size threshold\n"
|
||||
"More body text for entry one with enough length.\n\n"
|
||||
"## 11:00 — entry two with oversized body that forces per-entry chunking\n"
|
||||
+ "A"
|
||||
* 3000
|
||||
+ "\n\n"
|
||||
"## 12:00 — entry three with body added for adequate test fixture size\n"
|
||||
"More body text for entry three to make it realistic.\n"
|
||||
)
|
||||
palace_dir = tmp_path / "palace"
|
||||
|
||||
class _RaisingCollection:
|
||||
def __init__(self, real):
|
||||
self._real = real
|
||||
self.upsert_call_count = 0
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._real, name)
|
||||
|
||||
def upsert(self, **kwargs):
|
||||
self.upsert_call_count += 1
|
||||
raise RuntimeError("simulated embedding model failure")
|
||||
|
||||
real_get_collection = diary_ingest.get_collection
|
||||
raising_holder = {}
|
||||
|
||||
def _wrap_get_collection(path):
|
||||
real = real_get_collection(path)
|
||||
wrapped = _RaisingCollection(real)
|
||||
raising_holder["col"] = wrapped
|
||||
return wrapped
|
||||
|
||||
monkeypatch.setattr(diary_ingest, "get_collection", _wrap_get_collection)
|
||||
|
||||
with pytest.raises(RuntimeError, match="simulated embedding model failure"):
|
||||
diary_ingest.ingest_diaries(str(diary_dir), str(palace_dir), force=True)
|
||||
|
||||
# Exactly one batched upsert attempt — not three per-loop calls.
|
||||
# Fixture has a single diary file, so one batch per file == one
|
||||
# total. With a multi-file fixture this would be N batches.
|
||||
assert raising_holder["col"].upsert_call_count == 1, (
|
||||
"diary ingest must accumulate one batch per file; "
|
||||
f"got {raising_holder['col'].upsert_call_count} upsert calls"
|
||||
)
|
||||
|
||||
# Re-open the real collection (untouched by the failed write) and
|
||||
# confirm nothing landed for this source file.
|
||||
real_col = real_get_collection(str(palace_dir))
|
||||
rows = real_col.get(where={"source_file": str(diary_dir / "2026-05-18.md")})
|
||||
assert rows["ids"] == [], (
|
||||
f"failed batched upsert must leave no partial drawers; got {rows['ids']}"
|
||||
)
|
||||
|
||||
|
||||
# ── cross-wing tunnels ───────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestTunnels:
|
||||
"""Tunnels are explicit cross-wing connections stored in
|
||||
``~/.mempalace/tunnels.json``. Each test points the module-level
|
||||
``_TUNNEL_FILE`` at a fresh tmp file so tests don't cross-contaminate
|
||||
or touch the user's real tunnels."""
|
||||
``<palace_path_parent>/tunnels.json``. Each test points the resolver at
|
||||
a fresh tmp file so tests don't cross-contaminate or touch the user's
|
||||
real tunnels."""
|
||||
|
||||
def setup_method(self):
|
||||
import mempalace.palace_graph as pg
|
||||
|
||||
self._orig = pg._TUNNEL_FILE
|
||||
self._pg = pg
|
||||
self._orig_get = pg._get_tunnel_file
|
||||
self._orig_legacy = pg._legacy_tunnel_file
|
||||
self._tmpdir = tempfile.mkdtemp()
|
||||
pg._TUNNEL_FILE = os.path.join(self._tmpdir, "tunnels.json")
|
||||
self._tunnel_path = os.path.join(self._tmpdir, "tunnels.json")
|
||||
pg._get_tunnel_file = lambda *a, **kw: self._tunnel_path
|
||||
pg._legacy_tunnel_file = lambda: self._tunnel_path + ".legacy"
|
||||
# Neutralize the endpoint-existence check added for #1468 so these
|
||||
# legacy tests (which predate validation) don't get rejected when
|
||||
# an earlier test module has bound _get_collection to a real backend.
|
||||
self._orig_get_col = pg._get_collection
|
||||
pg._get_collection = lambda *a, **kw: None
|
||||
|
||||
def teardown_method(self):
|
||||
import mempalace.palace_graph as pg
|
||||
|
||||
pg._TUNNEL_FILE = self._orig
|
||||
self._pg._get_tunnel_file = self._orig_get
|
||||
self._pg._legacy_tunnel_file = self._orig_legacy
|
||||
self._pg._get_collection = self._orig_get_col
|
||||
import shutil
|
||||
|
||||
shutil.rmtree(self._tmpdir, ignore_errors=True)
|
||||
@@ -863,7 +1154,7 @@ class TestTunnels:
|
||||
import mempalace.palace_graph as pg
|
||||
|
||||
# Simulate a crash that left a truncated file behind.
|
||||
with open(pg._TUNNEL_FILE, "w") as f:
|
||||
with open(pg._get_tunnel_file(), "w") as f:
|
||||
f.write("{not valid json")
|
||||
|
||||
# Load should return [] rather than raising.
|
||||
@@ -879,8 +1170,8 @@ class TestTunnels:
|
||||
import mempalace.palace_graph as pg
|
||||
|
||||
create_tunnel("wing_a", "r1", "wing_b", "r2")
|
||||
assert os.path.exists(pg._TUNNEL_FILE)
|
||||
assert not os.path.exists(pg._TUNNEL_FILE + ".tmp")
|
||||
assert os.path.exists(pg._get_tunnel_file())
|
||||
assert not os.path.exists(pg._get_tunnel_file() + ".tmp")
|
||||
|
||||
def test_concurrent_creates_preserve_all_tunnels(self):
|
||||
"""Regression: two concurrent create_tunnel calls must not clobber
|
||||
@@ -905,7 +1196,7 @@ class TestTunnels:
|
||||
assert not errors, f"worker raised: {errors}"
|
||||
tunnels = list_tunnels()
|
||||
assert len(tunnels) == 5, (
|
||||
f"expected 5 concurrent tunnels, got {len(tunnels)} — " "write race dropped some"
|
||||
f"expected 5 concurrent tunnels, got {len(tunnels)} — write race dropped some"
|
||||
)
|
||||
|
||||
def test_created_at_is_timezone_aware(self):
|
||||
|
||||
+231
-5
@@ -4,9 +4,14 @@ import shutil
|
||||
from pathlib import Path
|
||||
|
||||
import chromadb
|
||||
import pytest
|
||||
|
||||
from mempalace.convo_miner import mine_convos
|
||||
from mempalace.palace import file_already_mined
|
||||
from mempalace.convo_miner import (
|
||||
_is_ai_tool_path,
|
||||
_resolve_wing,
|
||||
mine_convos,
|
||||
)
|
||||
from mempalace.palace import MineAlreadyRunning, file_already_mined
|
||||
|
||||
|
||||
def test_convo_mining():
|
||||
@@ -172,9 +177,9 @@ def test_mine_convos_rebuilds_stale_drawers_after_schema_bump(capsys):
|
||||
# Second mine — version gate should trigger rebuild
|
||||
mine_convos(tmpdir, palace_path, wing="test")
|
||||
out = capsys.readouterr().out
|
||||
assert (
|
||||
"Files skipped (already filed): 0" in out
|
||||
), "stale drawers should force a rebuild, not a skip"
|
||||
assert "Files skipped (already filed): 0" in out, (
|
||||
"stale drawers should force a rebuild, not a skip"
|
||||
)
|
||||
|
||||
client = chromadb.PersistentClient(path=palace_path)
|
||||
col = client.get_collection("mempalace_drawers")
|
||||
@@ -190,3 +195,224 @@ def test_mine_convos_rebuilds_stale_drawers_after_schema_bump(capsys):
|
||||
del col, client
|
||||
finally:
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
|
||||
|
||||
def _hold_palace_lock_in_child(palace_path, ready_flag, release_flag):
|
||||
"""Acquire mine_palace_lock in a child process and hold until signalled.
|
||||
|
||||
Cannot use threads because mine_palace_lock is intentionally re-entrant
|
||||
within a single thread (so ChromaCollection write methods can compose
|
||||
with miner.mine() without self-deadlock). The convos concurrency
|
||||
guarantee is across processes / threads, so the test has to mirror that.
|
||||
"""
|
||||
import os as _os
|
||||
import time as _time
|
||||
|
||||
from mempalace.palace import mine_palace_lock as _mpl
|
||||
|
||||
with _mpl(palace_path):
|
||||
open(ready_flag, "w").close()
|
||||
for _ in range(500):
|
||||
if _os.path.exists(release_flag):
|
||||
return
|
||||
_time.sleep(0.01)
|
||||
|
||||
|
||||
def test_mine_convos_refuses_concurrent_run_against_same_palace(tmp_path, monkeypatch):
|
||||
"""A second `mine_convos` against a palace currently being mined must
|
||||
raise MineAlreadyRunning, not stack up as a waiter that drives parallel
|
||||
ChromaDB writes. Mirrors the guarantee already given by `miner.mine`
|
||||
(see test_palace_locks.py) for the convos code path.
|
||||
"""
|
||||
import multiprocessing
|
||||
import time
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
convo_dir = tmp_path / "convos"
|
||||
convo_dir.mkdir()
|
||||
(convo_dir / "chat.txt").write_text("> q1\nshort answer.\n\n> q2\nanother short answer.\n")
|
||||
palace_path = str(tmp_path / "palace")
|
||||
ready_flag = str(tmp_path / "ready")
|
||||
release_flag = str(tmp_path / "release")
|
||||
|
||||
ctx = multiprocessing.get_context("spawn")
|
||||
holder = ctx.Process(
|
||||
target=_hold_palace_lock_in_child,
|
||||
args=(palace_path, ready_flag, release_flag),
|
||||
)
|
||||
holder.start()
|
||||
try:
|
||||
# Wait for the child to actually hold the lock before we attempt
|
||||
# to acquire from this process.
|
||||
for _ in range(500):
|
||||
if os.path.exists(ready_flag):
|
||||
break
|
||||
time.sleep(0.01)
|
||||
assert os.path.exists(ready_flag), "child never acquired palace lock"
|
||||
|
||||
with pytest.raises(MineAlreadyRunning):
|
||||
mine_convos(str(convo_dir), palace_path, wing="test")
|
||||
finally:
|
||||
open(release_flag, "w").close()
|
||||
holder.join(timeout=10)
|
||||
if holder.is_alive():
|
||||
holder.terminate()
|
||||
holder.join(timeout=5)
|
||||
|
||||
|
||||
def test_mine_convos_dry_run_bypasses_palace_lock(tmp_path, monkeypatch):
|
||||
"""Dry-run never writes to the palace, so it must coexist with a live
|
||||
mine instead of being blocked by the per-palace flock.
|
||||
"""
|
||||
import multiprocessing
|
||||
import time
|
||||
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
convo_dir = tmp_path / "convos"
|
||||
convo_dir.mkdir()
|
||||
(convo_dir / "chat.txt").write_text("> q1\nshort answer.\n\n> q2\nanother short answer.\n")
|
||||
palace_path = str(tmp_path / "palace")
|
||||
ready_flag = str(tmp_path / "ready_dry")
|
||||
release_flag = str(tmp_path / "release_dry")
|
||||
|
||||
ctx = multiprocessing.get_context("spawn")
|
||||
holder = ctx.Process(
|
||||
target=_hold_palace_lock_in_child,
|
||||
args=(palace_path, ready_flag, release_flag),
|
||||
)
|
||||
holder.start()
|
||||
try:
|
||||
for _ in range(500):
|
||||
if os.path.exists(ready_flag):
|
||||
break
|
||||
time.sleep(0.01)
|
||||
assert os.path.exists(ready_flag), "child never acquired palace lock"
|
||||
|
||||
# Must not raise — dry-run skips the lock entirely.
|
||||
mine_convos(str(convo_dir), palace_path, wing="test", dry_run=True)
|
||||
finally:
|
||||
open(release_flag, "w").close()
|
||||
holder.join(timeout=10)
|
||||
if holder.is_alive():
|
||||
holder.terminate()
|
||||
holder.join(timeout=5)
|
||||
|
||||
|
||||
# ── _is_ai_tool_path / _resolve_wing — wing_api auto-routing ───────────
|
||||
#
|
||||
# When a user runs `mempalace mine --mode convos` against a directory
|
||||
# inside a known AI-tool storage path (Claude Code's
|
||||
# ~/.claude/projects/, OpenAI Codex's ~/.codex/, Google Gemini CLI's
|
||||
# ~/.gemini/), the wing auto-defaults to "wing_api" rather than the
|
||||
# directory basename. This keeps API-sourced conversations grouped
|
||||
# under a single dedicated wing for visibility and privacy isolation.
|
||||
#
|
||||
# Explicit user-passed --wing always wins. Unrelated directories use
|
||||
# the existing basename fallback unchanged.
|
||||
|
||||
|
||||
def test_is_ai_tool_path_claude_projects_subdir(tmp_path):
|
||||
"""A subdirectory inside ~/.claude/projects/ is an AI tool path."""
|
||||
target = tmp_path / ".claude" / "projects" / "-Users-test-myapp"
|
||||
target.mkdir(parents=True)
|
||||
assert _is_ai_tool_path(target) is True
|
||||
|
||||
|
||||
def test_is_ai_tool_path_claude_projects_root(tmp_path):
|
||||
"""The ~/.claude/projects/ directory itself is an AI tool path."""
|
||||
target = tmp_path / ".claude" / "projects"
|
||||
target.mkdir(parents=True)
|
||||
assert _is_ai_tool_path(target) is True
|
||||
|
||||
|
||||
def test_is_ai_tool_path_codex_root(tmp_path):
|
||||
target = tmp_path / ".codex"
|
||||
target.mkdir()
|
||||
assert _is_ai_tool_path(target) is True
|
||||
|
||||
|
||||
def test_is_ai_tool_path_codex_sessions(tmp_path):
|
||||
"""Codex stores sessions under ~/.codex/sessions/YYYY/MM/DD/."""
|
||||
target = tmp_path / ".codex" / "sessions" / "2026" / "04" / "26"
|
||||
target.mkdir(parents=True)
|
||||
assert _is_ai_tool_path(target) is True
|
||||
|
||||
|
||||
def test_is_ai_tool_path_gemini_root(tmp_path):
|
||||
target = tmp_path / ".gemini"
|
||||
target.mkdir()
|
||||
assert _is_ai_tool_path(target) is True
|
||||
|
||||
|
||||
def test_is_ai_tool_path_gemini_chats(tmp_path):
|
||||
"""Gemini stores sessions under ~/.gemini/tmp/<hash>/chats/."""
|
||||
target = tmp_path / ".gemini" / "tmp" / "abc123" / "chats"
|
||||
target.mkdir(parents=True)
|
||||
assert _is_ai_tool_path(target) is True
|
||||
|
||||
|
||||
def test_is_ai_tool_path_dotclaude_without_projects_not_matched(tmp_path):
|
||||
"""`.claude/` alone (without `/projects`) is the settings dir, not a
|
||||
conversation source — it MUST NOT auto-route to wing_api."""
|
||||
target = tmp_path / ".claude"
|
||||
target.mkdir()
|
||||
assert _is_ai_tool_path(target) is False
|
||||
|
||||
|
||||
def test_is_ai_tool_path_unrelated_directory(tmp_path):
|
||||
target = tmp_path / "Documents" / "myproject"
|
||||
target.mkdir(parents=True)
|
||||
assert _is_ai_tool_path(target) is False
|
||||
|
||||
|
||||
def test_is_ai_tool_path_substring_no_false_positive(tmp_path):
|
||||
"""A directory NAMED like `.gemini-backup` or `.codex-archive` is NOT
|
||||
a real AI tool path. We use exact-segment match, not substring."""
|
||||
a = tmp_path / ".gemini-backup"
|
||||
a.mkdir()
|
||||
b = tmp_path / ".codex-archive"
|
||||
b.mkdir()
|
||||
assert _is_ai_tool_path(a) is False
|
||||
assert _is_ai_tool_path(b) is False
|
||||
|
||||
|
||||
def test_resolve_wing_explicit_wins_over_auto_detection(tmp_path):
|
||||
"""User-passed --wing always wins, even on an AI tool path."""
|
||||
target = tmp_path / ".claude" / "projects" / "-Users-x"
|
||||
target.mkdir(parents=True)
|
||||
assert _resolve_wing(target, wing="my_custom_wing") == "my_custom_wing"
|
||||
|
||||
|
||||
def test_resolve_wing_claude_projects_auto_routes_to_wing_api(tmp_path):
|
||||
target = tmp_path / ".claude" / "projects" / "-Users-test-myapp"
|
||||
target.mkdir(parents=True)
|
||||
assert _resolve_wing(target, wing=None) == "wing_api"
|
||||
|
||||
|
||||
def test_resolve_wing_codex_auto_routes_to_wing_api(tmp_path):
|
||||
target = tmp_path / ".codex" / "sessions" / "2026"
|
||||
target.mkdir(parents=True)
|
||||
assert _resolve_wing(target, wing=None) == "wing_api"
|
||||
|
||||
|
||||
def test_resolve_wing_gemini_auto_routes_to_wing_api(tmp_path):
|
||||
target = tmp_path / ".gemini" / "tmp" / "abc" / "chats"
|
||||
target.mkdir(parents=True)
|
||||
assert _resolve_wing(target, wing=None) == "wing_api"
|
||||
|
||||
|
||||
def test_resolve_wing_unrelated_dir_uses_basename_fallback(tmp_path):
|
||||
"""Existing behavior preserved: arbitrary directories use the
|
||||
sanitized basename as the wing."""
|
||||
target = tmp_path / "MyProject Folder"
|
||||
target.mkdir()
|
||||
# Spaces become underscores, hyphens become underscores, lowercased.
|
||||
assert _resolve_wing(target, wing=None) == "myproject_folder"
|
||||
|
||||
|
||||
def test_resolve_wing_empty_string_treated_as_no_wing(tmp_path):
|
||||
"""An empty string for wing should behave like None — fall through to
|
||||
auto-detection / basename. Mirrors the original `if not wing:` guard."""
|
||||
target = tmp_path / ".gemini" / "tmp"
|
||||
target.mkdir(parents=True)
|
||||
assert _resolve_wing(target, wing="") == "wing_api"
|
||||
|
||||
@@ -6,6 +6,8 @@ import sys
|
||||
import pytest
|
||||
|
||||
from mempalace.convo_miner import (
|
||||
CHUNK_SIZE,
|
||||
_emit_bounded,
|
||||
_file_chunks_locked,
|
||||
chunk_exchanges,
|
||||
detect_convo_room,
|
||||
@@ -38,11 +40,32 @@ class TestChunkExchanges:
|
||||
assert len(chunks) >= 2
|
||||
|
||||
def test_paragraph_line_group_fallback(self):
|
||||
"""Long content with no paragraph breaks chunks by line groups."""
|
||||
"""Long content with no paragraph breaks chunks by line groups.
|
||||
|
||||
Each emitted drawer must respect CHUNK_SIZE. Before #1534 the
|
||||
fallback chunker emitted one drawer per 25-line group without
|
||||
a size cap, so a 25-line group of long lines produced an
|
||||
oversized drawer that crashed embedding upsert.
|
||||
"""
|
||||
lines = [f"Line {i}: some content that is meaningful" for i in range(60)]
|
||||
content = "\n".join(lines)
|
||||
chunks = chunk_exchanges(content)
|
||||
assert len(chunks) >= 1
|
||||
max_len = max(len(c["content"]) for c in chunks)
|
||||
assert max_len <= CHUNK_SIZE, f"oversized chunk: max_len={max_len}"
|
||||
|
||||
def test_line_group_fallback_drops_sub_min_trailing_group(self):
|
||||
"""A trailing line-group whose stripped length is at or below
|
||||
MIN_CHUNK_SIZE must be dropped, not emitted as a tiny drawer."""
|
||||
lines = [f"Line {i}" for i in range(51)]
|
||||
content = "\n".join(lines)
|
||||
chunks = chunk_exchanges(content)
|
||||
from mempalace.convo_miner import MIN_CHUNK_SIZE
|
||||
|
||||
assert len(chunks) == 2, (
|
||||
f"expected 2 drawers (groups 0-24 and 25-49); got {len(chunks)}; "
|
||||
f"the single-line tail group should drop below MIN_CHUNK_SIZE={MIN_CHUNK_SIZE}"
|
||||
)
|
||||
|
||||
def test_empty_content(self):
|
||||
chunks = chunk_exchanges("")
|
||||
@@ -97,6 +120,136 @@ class TestChunkExchanges:
|
||||
for i in range(1, 14):
|
||||
assert f"Step {i}:" in stored, f"Step {i} was truncated and not stored"
|
||||
|
||||
def test_paragraph_loop_enforces_chunk_size(self):
|
||||
"""A paragraph longer than CHUNK_SIZE must split into multiple
|
||||
bounded drawers. Regression for #1534: the paragraph loop in
|
||||
``_chunk_by_paragraph`` used to append each paragraph as a
|
||||
single drawer regardless of size, producing one giant chunk
|
||||
that crashed embedding upsert with
|
||||
``RuntimeError: Invalid buffer size: ... GiB``.
|
||||
"""
|
||||
big_para = "x" * 5000
|
||||
tail = "small paragraph tail of meaningful length"
|
||||
content = big_para + "\n\n" + tail
|
||||
chunks = chunk_exchanges(content)
|
||||
max_len = max(len(c["content"]) for c in chunks)
|
||||
assert max_len <= CHUNK_SIZE, f"oversized chunk: max_len={max_len}"
|
||||
assert len(chunks) > 1, "5000-char content should produce multiple drawers"
|
||||
assert chunks[-1]["content"] == tail, (
|
||||
"trailing paragraph must be preserved as the last drawer"
|
||||
)
|
||||
|
||||
def test_custom_chunk_size_propagates_to_paragraph_path(self):
|
||||
"""User-supplied chunk_size must govern the paragraph chunker, not
|
||||
only the exchange chunker. Confirms config plumbing reaches both
|
||||
paths after the #1534 fix.
|
||||
"""
|
||||
big_para = "y" * 3000
|
||||
content = big_para + "\n\ntail paragraph of meaningful length"
|
||||
chunks = chunk_exchanges(content, chunk_size=400)
|
||||
max_len = max(len(c["content"]) for c in chunks)
|
||||
assert max_len <= 400, f"oversized chunk under custom chunk_size=400: {max_len}"
|
||||
|
||||
def test_paragraph_loop_no_content_loss(self):
|
||||
"""Verbatim principle: every char of a single long paragraph lands
|
||||
in some drawer in order. The slicing helper must not drop or
|
||||
reorder content."""
|
||||
content = "a" * 5000
|
||||
chunks = chunk_exchanges(content)
|
||||
joined = "".join(c["content"] for c in chunks)
|
||||
assert joined == content
|
||||
|
||||
def test_chunk_exactly_at_size_boundary(self):
|
||||
"""Content length == CHUNK_SIZE produces exactly one drawer of CHUNK_SIZE."""
|
||||
content = "z" * CHUNK_SIZE
|
||||
chunks = chunk_exchanges(content)
|
||||
assert len(chunks) == 1
|
||||
assert len(chunks[0]["content"]) == CHUNK_SIZE
|
||||
|
||||
def test_chunk_many_multiples_of_size(self):
|
||||
"""Content length == 8 * CHUNK_SIZE produces exactly 8 drawers, each
|
||||
of length CHUNK_SIZE."""
|
||||
content = "w" * (8 * CHUNK_SIZE)
|
||||
chunks = chunk_exchanges(content)
|
||||
assert len(chunks) == 8
|
||||
assert all(len(c["content"]) == CHUNK_SIZE for c in chunks)
|
||||
|
||||
def test_paragraph_loop_preserves_slice_order(self):
|
||||
"""Slices must appear in source order. Guards against a future
|
||||
regression where the helper reverses, shuffles, or duplicates
|
||||
slices — the verbatim invariant in CLAUDE.md depends on order
|
||||
as well as content."""
|
||||
content = "a" * CHUNK_SIZE + "b" * CHUNK_SIZE + "c" * CHUNK_SIZE
|
||||
chunks = chunk_exchanges(content)
|
||||
assert len(chunks) == 3
|
||||
assert chunks[0]["content"] == "a" * CHUNK_SIZE
|
||||
assert chunks[1]["content"] == "b" * CHUNK_SIZE
|
||||
assert chunks[2]["content"] == "c" * CHUNK_SIZE
|
||||
|
||||
|
||||
class TestEmitBounded:
|
||||
"""Direct unit tests for the chunk-size-enforcement helper."""
|
||||
|
||||
def test_emits_no_oversized_chunks(self):
|
||||
chunks = []
|
||||
_emit_bounded(chunks, "abc" * 20, chunk_size=10, min_chunk_size=0)
|
||||
assert all(len(c["content"]) <= 10 for c in chunks)
|
||||
|
||||
def test_assigns_sequential_chunk_indices(self):
|
||||
chunks = []
|
||||
_emit_bounded(chunks, "x" * 25, chunk_size=10, min_chunk_size=0)
|
||||
assert [c["chunk_index"] for c in chunks] == [0, 1, 2]
|
||||
|
||||
def test_continues_existing_chunk_index(self):
|
||||
chunks = [{"content": "pre-existing entry", "chunk_index": 0}]
|
||||
_emit_bounded(chunks, "y" * 5, chunk_size=10, min_chunk_size=0)
|
||||
assert len(chunks) == 2
|
||||
assert chunks[1]["chunk_index"] == 1
|
||||
|
||||
def test_empty_content_noop(self):
|
||||
chunks = []
|
||||
_emit_bounded(chunks, "", chunk_size=10, min_chunk_size=0)
|
||||
assert chunks == []
|
||||
|
||||
def test_small_trailing_slice_preserved(self):
|
||||
"""Once the whole content passes the floor, every slice is emitted
|
||||
verbatim so small trailing remainders are not silently dropped.
|
||||
Regression test for the data-loss class flagged on PR #1538."""
|
||||
chunks = []
|
||||
_emit_bounded(chunks, "z" * 23, chunk_size=10, min_chunk_size=5)
|
||||
assert len(chunks) == 3
|
||||
assert [len(c["content"]) for c in chunks] == [10, 10, 3]
|
||||
assert "".join(c["content"] for c in chunks) == "z" * 23
|
||||
|
||||
def test_trailing_whitespace_slice_preserved_when_whole_passes(self):
|
||||
"""When the whole content passes the floor, a trailing
|
||||
whitespace-only slice is preserved verbatim rather than dropped.
|
||||
The floor is a noise filter on the WHOLE input, not a per-slice gate."""
|
||||
chunks = []
|
||||
_emit_bounded(chunks, "a" * 10 + " " * 10, chunk_size=10, min_chunk_size=5)
|
||||
assert len(chunks) == 2
|
||||
assert chunks[0]["content"] == "a" * 10
|
||||
assert chunks[1]["content"] == " " * 10
|
||||
|
||||
def test_whole_content_below_floor_dropped(self):
|
||||
"""The floor is applied to the stripped whole content. An all-whitespace
|
||||
input (stripped length 0) or a too-short input is dropped without slicing."""
|
||||
chunks = []
|
||||
_emit_bounded(chunks, " " * 100, chunk_size=10, min_chunk_size=5)
|
||||
_emit_bounded(chunks, "ab", chunk_size=10, min_chunk_size=5)
|
||||
assert chunks == []
|
||||
|
||||
def test_split_805_chars_at_chunk_size_800_preserves_tail(self):
|
||||
"""805 chars at chunk_size=800 produces a 5-char tail. With the
|
||||
whole-content floor (not per-slice), the 5-char tail is preserved
|
||||
verbatim. Directly addresses the data-loss scenario raised on PR #1538."""
|
||||
chunks = []
|
||||
_emit_bounded(chunks, "y" * 805, chunk_size=800, min_chunk_size=30)
|
||||
assert len(chunks) == 2
|
||||
assert chunks[0]["content"] == "y" * 800
|
||||
assert chunks[1]["content"] == "y" * 5
|
||||
assert "".join(c["content"] for c in chunks) == "y" * 805
|
||||
|
||||
|
||||
class TestDetectConvoRoom:
|
||||
def test_technical_room(self):
|
||||
|
||||
+21
-21
@@ -112,17 +112,17 @@ class TestHeuristic:
|
||||
"anthropic's haiku model is cheap enough to run on every drawer",
|
||||
]
|
||||
result = detect_origin_heuristic(samples)
|
||||
assert (
|
||||
result.likely_ai_dialogue is True
|
||||
), f"lowercase brand terms missed; evidence: {result.evidence}"
|
||||
assert result.likely_ai_dialogue is True, (
|
||||
f"lowercase brand terms missed; evidence: {result.evidence}"
|
||||
)
|
||||
# Evidence must show MULTIPLE distinct case-insensitive brand matches.
|
||||
# 'chatgpt' lowercase only matches under case-insensitive search
|
||||
# (the brand list has 'ChatGPT' proper-cased only).
|
||||
evidence_str = " ".join(result.evidence).lower()
|
||||
matched = sum(t in evidence_str for t in ("chatgpt", "anthropic", "haiku", "gemini-pro"))
|
||||
assert (
|
||||
matched >= 2
|
||||
), f"case-insensitive brand matches did not fire — only got: {result.evidence}"
|
||||
assert matched >= 2, (
|
||||
f"case-insensitive brand matches did not fire — only got: {result.evidence}"
|
||||
)
|
||||
|
||||
def test_zodiac_corpus_not_flagged_as_ai(self):
|
||||
"""An astrology forum post with high 'Gemini' density but ZERO
|
||||
@@ -138,9 +138,9 @@ class TestHeuristic:
|
||||
"The Gemini twins in Greek mythology are Castor and Pollux.",
|
||||
]
|
||||
result = detect_origin_heuristic(samples)
|
||||
assert (
|
||||
result.likely_ai_dialogue is False
|
||||
), f"zodiac corpus wrongly flagged AI; evidence: {result.evidence}"
|
||||
assert result.likely_ai_dialogue is False, (
|
||||
f"zodiac corpus wrongly flagged AI; evidence: {result.evidence}"
|
||||
)
|
||||
|
||||
def test_french_novel_with_claude_name_not_flagged(self):
|
||||
"""A French novel where 'Claude' is a character name (Claude is a
|
||||
@@ -155,9 +155,9 @@ class TestHeuristic:
|
||||
"Les amis de Claude s'étaient réunis chez lui pour fêter ses soixante ans.",
|
||||
]
|
||||
result = detect_origin_heuristic(samples)
|
||||
assert (
|
||||
result.likely_ai_dialogue is False
|
||||
), f"French novel wrongly flagged AI; evidence: {result.evidence}"
|
||||
assert result.likely_ai_dialogue is False, (
|
||||
f"French novel wrongly flagged AI; evidence: {result.evidence}"
|
||||
)
|
||||
|
||||
def test_poetry_corpus_with_haiku_sonnet_not_flagged(self):
|
||||
"""A poetry corpus with high 'haiku', 'sonnet', 'opus' density
|
||||
@@ -172,9 +172,9 @@ class TestHeuristic:
|
||||
"Her first opus, published at twenty, was a song cycle for soprano.",
|
||||
]
|
||||
result = detect_origin_heuristic(samples)
|
||||
assert (
|
||||
result.likely_ai_dialogue is False
|
||||
), f"poetry corpus wrongly flagged AI; evidence: {result.evidence}"
|
||||
assert result.likely_ai_dialogue is False, (
|
||||
f"poetry corpus wrongly flagged AI; evidence: {result.evidence}"
|
||||
)
|
||||
|
||||
def test_word_boundary_brand_matching(self):
|
||||
"""Brand-term matching must use word boundaries. Embedded matches
|
||||
@@ -208,9 +208,9 @@ class TestHeuristic:
|
||||
)
|
||||
|
||||
# And classification should be not-AI (no real AI signals present).
|
||||
assert (
|
||||
result.likely_ai_dialogue is False
|
||||
), f"corpus has no real AI signals; evidence: {result.evidence}"
|
||||
assert result.likely_ai_dialogue is False, (
|
||||
f"corpus has no real AI signals; evidence: {result.evidence}"
|
||||
)
|
||||
|
||||
def test_ambiguous_brand_with_unambiguous_signal_flagged(self):
|
||||
"""When an ambiguous brand term ('Gemini') co-occurs with an
|
||||
@@ -224,9 +224,9 @@ class TestHeuristic:
|
||||
"MCP server config: Gemini as primary, OpenAI as fallback.",
|
||||
]
|
||||
result = detect_origin_heuristic(samples)
|
||||
assert (
|
||||
result.likely_ai_dialogue is True
|
||||
), f"ambiguous+unambiguous co-occurrence missed; evidence: {result.evidence}"
|
||||
assert result.likely_ai_dialogue is True, (
|
||||
f"ambiguous+unambiguous co-occurrence missed; evidence: {result.evidence}"
|
||||
)
|
||||
|
||||
|
||||
# ── Tier 2: LLM-assisted (mocked) ─────────────────────────────────────────
|
||||
|
||||
@@ -178,7 +178,7 @@ def test_corpus_origin_reclassifies_personas(
|
||||
# All three personas land in the new bucket.
|
||||
expected_personas = {"Echo", "Sparrow", "Cipher"}
|
||||
assert expected_personas <= persona_names_in_bucket, (
|
||||
f"Expected all three personas in agent_personas, got: " f"{persona_names_in_bucket}"
|
||||
f"Expected all three personas in agent_personas, got: {persona_names_in_bucket}"
|
||||
)
|
||||
|
||||
# And NONE of them remain in the people bucket.
|
||||
@@ -293,8 +293,7 @@ def test_init_pass_zero_writes_origin_json_to_palace(ai_dialogue_corpus: Path, t
|
||||
assert isinstance(data["result"].get("likely_ai_dialogue"), bool)
|
||||
# Fixture is heavy AI-dialogue — heuristic should classify as such.
|
||||
assert data["result"]["likely_ai_dialogue"] is True, (
|
||||
"Heuristic should classify the AI-dialogue fixture as AI-dialogue. "
|
||||
f"Got: {data['result']}"
|
||||
f"Heuristic should classify the AI-dialogue fixture as AI-dialogue. Got: {data['result']}"
|
||||
)
|
||||
|
||||
|
||||
@@ -744,9 +743,9 @@ def test_init_graceful_fallback_when_provider_unavailable(
|
||||
out = capsys.readouterr().out
|
||||
# The fallback message should mention how to silence (--no-llm) so the
|
||||
# user knows what flipped.
|
||||
assert (
|
||||
"no-llm" in out.lower() or "--no-llm" in out
|
||||
), f"Graceful fallback message must point at --no-llm. Got: {out!r}"
|
||||
assert "no-llm" in out.lower() or "--no-llm" in out, (
|
||||
f"Graceful fallback message must point at --no-llm. Got: {out!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_init_graceful_fallback_on_provider_construction_error(
|
||||
@@ -1308,9 +1307,9 @@ def test_integration_llm_refine_corpus_origin_preamble_does_not_break_topic_labe
|
||||
"TOPIC label instructions disappeared from SYSTEM_PROMPT — "
|
||||
"corpus-origin preamble appears to have replaced rather than appended"
|
||||
)
|
||||
assert (
|
||||
"CORPUS CONTEXT" in captured["system"]
|
||||
), "corpus-origin corpus context preamble missing from prompt"
|
||||
assert "CORPUS CONTEXT" in captured["system"], (
|
||||
"corpus-origin corpus context preamble missing from prompt"
|
||||
)
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────
|
||||
@@ -1546,12 +1545,12 @@ def test_merge_tier_fields_heuristic_no_no_personas_leak():
|
||||
|
||||
assert wrapped is not None
|
||||
res = wrapped["result"]
|
||||
assert (
|
||||
res["likely_ai_dialogue"] is False
|
||||
), f"Both tiers said NOT AI-dialogue; merged result must be False. Got: {res}"
|
||||
assert (
|
||||
res["agent_persona_names"] == []
|
||||
), f"No personas should leak when both tiers report none. Got: {res}"
|
||||
assert res["likely_ai_dialogue"] is False, (
|
||||
f"Both tiers said NOT AI-dialogue; merged result must be False. Got: {res}"
|
||||
)
|
||||
assert res["agent_persona_names"] == [], (
|
||||
f"No personas should leak when both tiers report none. Got: {res}"
|
||||
)
|
||||
# Heuristic owns confidence. Mocked LLM returned 0.95; heuristic's
|
||||
# narrative-branch confidence is 0.9. Verifying we kept 0.9 catches
|
||||
# any future regression that lets LLM confidence override heuristic.
|
||||
@@ -1609,24 +1608,23 @@ def test_merge_tier_fields_heuristic_yes_llm_yes_combines_evidence():
|
||||
# LLM produced its own; the merged result should include both signal
|
||||
# trails for audit purposes.
|
||||
evidence_text = " ".join(res["evidence"])
|
||||
assert (
|
||||
"LLM-extracted" in evidence_text
|
||||
), f"LLM evidence string missing from merged result. Got: {res['evidence']}"
|
||||
assert "LLM-extracted" in evidence_text, (
|
||||
f"LLM evidence string missing from merged result. Got: {res['evidence']}"
|
||||
)
|
||||
# Heuristic always produces at least one evidence line for AI-dialogue
|
||||
# input (brand-term match), so the combined list has more than just LLM's.
|
||||
assert len(res["evidence"]) >= 2, (
|
||||
f"Combined evidence should include both heuristic + LLM lines. " f"Got: {res['evidence']}"
|
||||
f"Combined evidence should include both heuristic + LLM lines. Got: {res['evidence']}"
|
||||
)
|
||||
# Each entry must carry its tier prefix so on-disk origin.json is
|
||||
# auditable — readers can tell which tier produced which signal line.
|
||||
tier1_lines = [e for e in res["evidence"] if e.startswith("Tier-1 heuristic: ")]
|
||||
tier2_lines = [e for e in res["evidence"] if e.startswith("Tier-2 LLM: ")]
|
||||
assert tier1_lines, (
|
||||
f"Expected at least one 'Tier-1 heuristic: ' prefixed evidence line. "
|
||||
f"Got: {res['evidence']}"
|
||||
f"Expected at least one 'Tier-1 heuristic: ' prefixed evidence line. Got: {res['evidence']}"
|
||||
)
|
||||
assert tier2_lines, (
|
||||
f"Expected at least one 'Tier-2 LLM: ' prefixed evidence line. " f"Got: {res['evidence']}"
|
||||
f"Expected at least one 'Tier-2 LLM: ' prefixed evidence line. Got: {res['evidence']}"
|
||||
)
|
||||
# Every entry should be tier-prefixed (no untagged passthrough).
|
||||
untagged = [
|
||||
@@ -1759,11 +1757,11 @@ def test_init_prints_privacy_warning_when_provider_is_external(
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "EXTERNAL API" in out, (
|
||||
f"Privacy warning must mention 'EXTERNAL API' when provider is external. " f"Got: {out!r}"
|
||||
f"Privacy warning must mention 'EXTERNAL API' when provider is external. Got: {out!r}"
|
||||
)
|
||||
assert "--no-llm" in out, (
|
||||
f"Privacy warning must point users at --no-llm to opt out. Got: {out!r}"
|
||||
)
|
||||
assert (
|
||||
"--no-llm" in out
|
||||
), f"Privacy warning must point users at --no-llm to opt out. Got: {out!r}"
|
||||
# The warning should also tell users MemPalace isn't responsible
|
||||
# for downstream provider behavior.
|
||||
assert (
|
||||
@@ -1804,7 +1802,7 @@ def test_init_no_privacy_warning_when_provider_is_local(
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "EXTERNAL API" not in out, (
|
||||
f"Privacy warning fired for a LOCAL provider — should not have. " f"Got: {out!r}"
|
||||
f"Privacy warning fired for a LOCAL provider — should not have. Got: {out!r}"
|
||||
)
|
||||
|
||||
|
||||
@@ -1827,9 +1825,9 @@ def test_init_no_privacy_warning_with_no_llm_flag(ai_dialogue_corpus: Path, tmp_
|
||||
|
||||
mock_get.assert_not_called(), "--no-llm must short-circuit before provider acquisition"
|
||||
out = capsys.readouterr().out
|
||||
assert (
|
||||
"EXTERNAL API" not in out
|
||||
), f"Privacy warning fired on --no-llm path — should not have. Got: {out!r}"
|
||||
assert "EXTERNAL API" not in out, (
|
||||
f"Privacy warning fired on --no-llm path — should not have. Got: {out!r}"
|
||||
)
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────
|
||||
@@ -1988,9 +1986,9 @@ def test_init_accept_external_llm_flag_bypasses_consent_prompt(
|
||||
"--accept-external-llm must bypass the consent prompt for "
|
||||
"non-interactive / CI use. input() was called anyway."
|
||||
)
|
||||
assert (
|
||||
fake_provider.classify.called
|
||||
), "With --accept-external-llm, init must proceed with the LLM."
|
||||
assert fake_provider.classify.called, (
|
||||
"With --accept-external-llm, init must proceed with the LLM."
|
||||
)
|
||||
|
||||
|
||||
def test_init_no_consent_prompt_when_endpoint_is_local(
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -246,3 +246,67 @@ def test_positive_words():
|
||||
def test_negative_words():
|
||||
assert "bug" in NEGATIVE_WORDS
|
||||
assert "crash" in NEGATIVE_WORDS
|
||||
|
||||
|
||||
# ── extract_memories — oversized segment chunking (#1539) ──────────────
|
||||
|
||||
|
||||
def test_extract_memories_oversized_segment_slices_with_label_preserved():
|
||||
"""Regression for #1539: a segment longer than chunk_size must be
|
||||
split into multiple memories with the same memory_type. Joined
|
||||
slices must equal the original (verbatim store per CLAUDE.md)."""
|
||||
decision_phrase = "We decided to migrate to PostgreSQL because performance matters. "
|
||||
long_segment = decision_phrase * 50 # ~3,200 chars, well above default 800
|
||||
memories = extract_memories(long_segment)
|
||||
assert len(memories) > 1, (
|
||||
f"oversized segment must split into multiple slices; got {len(memories)}"
|
||||
)
|
||||
assert all(len(m["content"]) <= 800 for m in memories), (
|
||||
f"all slices must be <= chunk_size=800; got max={max(len(m['content']) for m in memories)}"
|
||||
)
|
||||
types = {m["memory_type"] for m in memories}
|
||||
assert len(types) == 1, f"all slices must share one memory_type; got {types}"
|
||||
assert "decision" in types
|
||||
joined = "".join(m["content"] for m in memories)
|
||||
assert joined == long_segment.strip(), (
|
||||
"joined slices must equal original (after strip) verbatim"
|
||||
)
|
||||
|
||||
|
||||
def test_extract_memories_oversized_segment_with_custom_chunk_size():
|
||||
"""Regression for #1539: caller-supplied chunk_size must govern the
|
||||
paragraph slicer in extract_memories."""
|
||||
decision_phrase = "We decided on Redis because we measured the latency profile. "
|
||||
long_segment = decision_phrase * 40 # ~2,500 chars
|
||||
memories = extract_memories(long_segment, chunk_size=400)
|
||||
assert len(memories) > 1
|
||||
assert all(len(m["content"]) <= 400 for m in memories), (
|
||||
f"all slices must be <= 400; got max={max(len(m['content']) for m in memories)}"
|
||||
)
|
||||
|
||||
|
||||
def test_extract_memories_normal_segment_unchanged():
|
||||
"""Regression catch: a segment under chunk_size must produce
|
||||
exactly one memory (existing pre-#1539 behaviour for sub-cap)."""
|
||||
text = (
|
||||
"We decided to use React because it fits our team workflow and "
|
||||
"the migration path from our existing stack is clear."
|
||||
)
|
||||
memories = extract_memories(text)
|
||||
assert len(memories) == 1
|
||||
assert memories[0]["content"] == text
|
||||
assert memories[0]["memory_type"] == "decision"
|
||||
|
||||
|
||||
def test_extract_memories_chunk_index_contiguous_across_segments():
|
||||
"""Regression for #1539: chunk_index must be sequential 0,1,2,...
|
||||
across mixed normal + oversized segments without gaps."""
|
||||
decision_phrase = "We decided to choose Postgres because the index plan works. "
|
||||
long_segment = decision_phrase * 30 # ~1,800 chars → multiple slices at 800
|
||||
second_short = "I prefer always writing tests first because it shapes the API better."
|
||||
text = long_segment + "\n\n" + second_short
|
||||
memories = extract_memories(text)
|
||||
indices = [m["chunk_index"] for m in memories]
|
||||
assert indices == list(range(len(memories))), (
|
||||
f"chunk_index must be 0..N-1 sequential; got {indices}"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
"""Tests for the within-wing hallway primitive.
|
||||
|
||||
Hallways are bridges INSIDE a wing that connect entities (people,
|
||||
projects, concepts, interests) to each other, materialized from
|
||||
drawer-level co-occurrence. Two entities are linked by a hallway when
|
||||
they appear together in enough drawers across the wing.
|
||||
|
||||
This file is RED-first. The corresponding implementation lives in
|
||||
``mempalace/hallways.py`` and is written to make these tests pass.
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
|
||||
# Mock chromadb at import time so the hallways module can be loaded even
|
||||
# in environments where chromadb isn't installed. Mirrors the pattern in
|
||||
# ``tests/test_palace_graph_tunnels.py``.
|
||||
with patch.dict("sys.modules", {"chromadb": MagicMock()}):
|
||||
from mempalace import hallways as hallways_mod
|
||||
|
||||
|
||||
def _use_tmp_hallway_file(monkeypatch, tmp_path):
|
||||
"""Redirect hallway persistence to a per-test JSON file."""
|
||||
hallway_file = tmp_path / "hallways.json"
|
||||
monkeypatch.setattr(hallways_mod, "_HALLWAY_FILE", str(hallway_file))
|
||||
return hallway_file
|
||||
|
||||
|
||||
def _fake_collection(drawers):
|
||||
"""Build a MagicMock collection whose .get() returns the given drawer set."""
|
||||
col = MagicMock()
|
||||
metadatas = [d for d in drawers]
|
||||
ids = [f"drawer_{i}" for i in range(len(drawers))]
|
||||
col.get.return_value = {"ids": ids, "metadatas": metadatas}
|
||||
return col
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Storage primitives — _load_hallways / _save_hallways
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestHallwayStorage:
|
||||
def test_load_hallways_missing_file_returns_empty_list(self, tmp_path, monkeypatch):
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
assert hallways_mod._load_hallways() == []
|
||||
|
||||
def test_load_hallways_corrupt_file_returns_empty_list(self, tmp_path, monkeypatch):
|
||||
hallway_file = _use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
hallway_file.write_text("{not valid json", encoding="utf-8")
|
||||
assert hallways_mod._load_hallways() == []
|
||||
|
||||
def test_save_and_load_round_trip(self, tmp_path, monkeypatch):
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
sample = [
|
||||
{
|
||||
"id": "hallway_wing_aya_aya_lumi_abc12345",
|
||||
"wing": "wing_aya",
|
||||
"entity_a": "Aya",
|
||||
"entity_b": "Lumi",
|
||||
"co_occurrence_count": 47,
|
||||
"rooms": ["diary", "letters"],
|
||||
"label": "Aya ↔ Lumi (co-occur in 47 drawers across 2 rooms)",
|
||||
}
|
||||
]
|
||||
hallways_mod._save_hallways(sample)
|
||||
assert hallways_mod._load_hallways() == sample
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# compute_hallways_for_wing — entity-pair co-occurrence algorithm
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestComputeHallways:
|
||||
def test_returns_empty_for_unknown_wing(self, tmp_path, monkeypatch):
|
||||
"""Wing with no drawers → no hallways, no crash."""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection([])
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_nonexistent", col=col)
|
||||
assert result == []
|
||||
|
||||
def test_returns_empty_when_no_drawer_has_two_entities(self, tmp_path, monkeypatch):
|
||||
"""A drawer must mention >= 2 entities to contribute a pair."""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya"}, # only one
|
||||
{"wing": "wing_aya", "room": "diary", "entities": ""}, # none
|
||||
]
|
||||
)
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_aya", col=col)
|
||||
assert result == []
|
||||
|
||||
def test_creates_hallway_for_entity_pair_when_threshold_met(self, tmp_path, monkeypatch):
|
||||
"""Two entities co-occurring in >= min_count drawers → one hallway record.
|
||||
|
||||
With min_count=2, Aya↔Lumi appear together in 3 drawers; that's a hallway.
|
||||
"""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi"},
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi;Ever"},
|
||||
{"wing": "wing_aya", "room": "letters", "entities": "Aya;Lumi"},
|
||||
]
|
||||
)
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
# Find the Aya↔Lumi hallway (other pairs like Aya↔Ever might also be present)
|
||||
aya_lumi = [h for h in result if {h["entity_a"], h["entity_b"]} == {"Aya", "Lumi"}]
|
||||
assert len(aya_lumi) == 1
|
||||
hallway = aya_lumi[0]
|
||||
assert hallway["wing"] == "wing_aya"
|
||||
assert hallway["co_occurrence_count"] == 3
|
||||
assert set(hallway["rooms"]) == {"diary", "letters"}
|
||||
|
||||
def test_connects_person_to_concept(self, tmp_path, monkeypatch):
|
||||
"""Entities aren't only people — projects/concepts/interests count too.
|
||||
|
||||
The entity tag treats 'consciousness' the same as 'Aya'; both are
|
||||
just tokens in the drawer's entities field. So Aya↔consciousness is
|
||||
a valid hallway when they co-occur enough.
|
||||
"""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;consciousness"},
|
||||
{"wing": "wing_aya", "room": "research", "entities": "Aya;consciousness"},
|
||||
{"wing": "wing_aya", "room": "ideas", "entities": "Aya;consciousness;Lumi"},
|
||||
]
|
||||
)
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
aya_consciousness = [
|
||||
h for h in result if {h["entity_a"], h["entity_b"]} == {"Aya", "consciousness"}
|
||||
]
|
||||
assert len(aya_consciousness) == 1
|
||||
assert aya_consciousness[0]["co_occurrence_count"] == 3
|
||||
assert set(aya_consciousness[0]["rooms"]) == {"diary", "research", "ideas"}
|
||||
|
||||
def test_respects_min_count_threshold(self, tmp_path, monkeypatch):
|
||||
"""min_count=3 filters out pairs that only co-occur twice."""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi"},
|
||||
{"wing": "wing_aya", "room": "letters", "entities": "Aya;Lumi"},
|
||||
]
|
||||
)
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=3)
|
||||
assert result == []
|
||||
|
||||
def test_creates_deterministic_id_per_entity_pair(self, tmp_path, monkeypatch):
|
||||
"""Same wing + same entity pair → same hallway id (idempotent re-runs)."""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi"},
|
||||
{"wing": "wing_aya", "room": "letters", "entities": "Aya;Lumi"},
|
||||
]
|
||||
)
|
||||
first = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
second = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
# Find the Aya↔Lumi record in both runs; ids must match.
|
||||
f_id = next(h["id"] for h in first if {h["entity_a"], h["entity_b"]} == {"Aya", "Lumi"})
|
||||
s_id = next(h["id"] for h in second if {h["entity_a"], h["entity_b"]} == {"Aya", "Lumi"})
|
||||
assert f_id == s_id
|
||||
assert f_id.startswith("hallway_")
|
||||
|
||||
def test_entity_pair_is_symmetric(self, tmp_path, monkeypatch):
|
||||
"""Drawer says 'Aya;Lumi'; another says 'Lumi;Aya' — same hallway."""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi"},
|
||||
{"wing": "wing_aya", "room": "letters", "entities": "Lumi;Aya"},
|
||||
]
|
||||
)
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
aya_lumi = [h for h in result if {h["entity_a"], h["entity_b"]} == {"Aya", "Lumi"}]
|
||||
# Symmetry: the two drawers count as 2 co-occurrences, not 0 (no
|
||||
# double-bookkeeping despite the swapped order).
|
||||
assert len(aya_lumi) == 1
|
||||
assert aya_lumi[0]["co_occurrence_count"] == 2
|
||||
|
||||
def test_persists_to_json(self, tmp_path, monkeypatch):
|
||||
"""After compute, _load_hallways() returns the new records."""
|
||||
hallway_file = _use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi"},
|
||||
{"wing": "wing_aya", "room": "letters", "entities": "Aya;Lumi"},
|
||||
]
|
||||
)
|
||||
hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
assert hallway_file.exists()
|
||||
loaded = hallways_mod._load_hallways()
|
||||
assert any({h["entity_a"], h["entity_b"]} == {"Aya", "Lumi"} for h in loaded)
|
||||
|
||||
def test_tracks_rooms_across_co_occurrences(self, tmp_path, monkeypatch):
|
||||
"""A hallway records the set of rooms where its entities co-occurred."""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi"},
|
||||
{"wing": "wing_aya", "room": "letters", "entities": "Aya;Lumi"},
|
||||
{"wing": "wing_aya", "room": "diary", "entities": "Aya;Lumi"},
|
||||
]
|
||||
)
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
h = next(h for h in result if {h["entity_a"], h["entity_b"]} == {"Aya", "Lumi"})
|
||||
assert set(h["rooms"]) == {"diary", "letters"}
|
||||
assert h["co_occurrence_count"] == 3 # 3 drawers, not 3 rooms
|
||||
|
||||
def test_skips_sentinel_drawers(self, tmp_path, monkeypatch):
|
||||
"""Sentinels exist for file_already_mined() bookkeeping. Skip them."""
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
col = _fake_collection(
|
||||
[
|
||||
{
|
||||
"wing": "wing_aya",
|
||||
"room": "documents",
|
||||
"entities": "Aya;Lumi",
|
||||
"is_sentinel": True,
|
||||
},
|
||||
{
|
||||
"wing": "wing_aya",
|
||||
"room": "documents",
|
||||
"entities": "Aya;Lumi",
|
||||
"is_sentinel": True,
|
||||
},
|
||||
]
|
||||
)
|
||||
result = hallways_mod.compute_hallways_for_wing("wing_aya", col=col, min_count=2)
|
||||
assert result == []
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Query API — list_hallways, delete_hallway
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestHallwayQuery:
|
||||
def test_list_hallways_returns_all_when_no_filter(self, tmp_path, monkeypatch):
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
hallways_mod._save_hallways(
|
||||
[
|
||||
{"id": "h1", "wing": "wing_aya", "entity_a": "Aya", "entity_b": "Lumi"},
|
||||
{"id": "h2", "wing": "wing_lumi", "entity_a": "Lumi", "entity_b": "Ever"},
|
||||
]
|
||||
)
|
||||
assert len(hallways_mod.list_hallways()) == 2
|
||||
|
||||
def test_list_hallways_filters_by_wing(self, tmp_path, monkeypatch):
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
hallways_mod._save_hallways(
|
||||
[
|
||||
{"id": "h1", "wing": "wing_aya", "entity_a": "Aya", "entity_b": "Lumi"},
|
||||
{"id": "h2", "wing": "wing_lumi", "entity_a": "Lumi", "entity_b": "Ever"},
|
||||
]
|
||||
)
|
||||
result = hallways_mod.list_hallways(wing="wing_aya")
|
||||
assert len(result) == 1
|
||||
assert result[0]["id"] == "h1"
|
||||
|
||||
def test_delete_hallway_removes_record(self, tmp_path, monkeypatch):
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
hallways_mod._save_hallways(
|
||||
[
|
||||
{"id": "h1", "wing": "wing_aya", "entity_a": "Aya", "entity_b": "Lumi"},
|
||||
{"id": "h2", "wing": "wing_aya", "entity_a": "Aya", "entity_b": "Ever"},
|
||||
]
|
||||
)
|
||||
assert hallways_mod.delete_hallway("h1") is True
|
||||
remaining = hallways_mod._load_hallways()
|
||||
assert len(remaining) == 1
|
||||
assert remaining[0]["id"] == "h2"
|
||||
|
||||
def test_delete_hallway_unknown_id_returns_false(self, tmp_path, monkeypatch):
|
||||
_use_tmp_hallway_file(monkeypatch, tmp_path)
|
||||
hallways_mod._save_hallways([{"id": "h1", "wing": "wing_aya"}])
|
||||
assert hallways_mod.delete_hallway("nonexistent") is False
|
||||
@@ -98,12 +98,12 @@ class TestNoBash4OnlyBuiltins:
|
||||
@_BOTH_HOOKS
|
||||
def test_no_mapfile(self, hook):
|
||||
code = _hook_src_no_comments(hook)
|
||||
assert (
|
||||
"mapfile" not in code
|
||||
), f"{hook.name} uses mapfile, unavailable on macOS /bin/bash 3.2 (#1440)"
|
||||
assert (
|
||||
"readarray" not in code
|
||||
), f"{hook.name} uses readarray, unavailable on macOS /bin/bash 3.2 (#1440)"
|
||||
assert "mapfile" not in code, (
|
||||
f"{hook.name} uses mapfile, unavailable on macOS /bin/bash 3.2 (#1440)"
|
||||
)
|
||||
assert "readarray" not in code, (
|
||||
f"{hook.name} uses readarray, unavailable on macOS /bin/bash 3.2 (#1440)"
|
||||
)
|
||||
|
||||
@_BOTH_HOOKS
|
||||
def test_sed_extraction_present(self, hook):
|
||||
@@ -115,9 +115,9 @@ class TestNoBash4OnlyBuiltins:
|
||||
# method back to ``mapfile`` while keeping the old commentary.
|
||||
src = _hook_src_no_comments(hook)
|
||||
# Each hook reads at least two values via ``sed -n 'Np'`` (sentinel + session_id).
|
||||
assert (
|
||||
src.count("sed -n '") >= 2
|
||||
), f"{hook.name} must use sed -n 'Np' for POSIX-portable line extraction"
|
||||
assert src.count("sed -n '") >= 2, (
|
||||
f"{hook.name} must use sed -n 'Np' for POSIX-portable line extraction"
|
||||
)
|
||||
|
||||
@_BOTH_HOOKS
|
||||
def test_bash_syntax_clean(self, hook):
|
||||
@@ -150,9 +150,9 @@ class TestSessionIdExtraction:
|
||||
assert "WARN: input parse failed" not in log
|
||||
state_dir = tmp_path / ".mempalace" / "hook_state"
|
||||
assert not (state_dir / "last_input.log").exists()
|
||||
assert not (
|
||||
state_dir / "last_python_err.log"
|
||||
).exists(), "successful parse must leave no last_python_err.log behind"
|
||||
assert not (state_dir / "last_python_err.log").exists(), (
|
||||
"successful parse must leave no last_python_err.log behind"
|
||||
)
|
||||
|
||||
def test_precompact_hook_extracts_session_id(self, tmp_path):
|
||||
out, _ = _run_hook(
|
||||
@@ -166,9 +166,9 @@ class TestSessionIdExtraction:
|
||||
assert "WARN: input parse failed" not in log
|
||||
state_dir = tmp_path / ".mempalace" / "hook_state"
|
||||
assert not (state_dir / "last_input.log").exists()
|
||||
assert not (
|
||||
state_dir / "last_python_err.log"
|
||||
).exists(), "successful parse must leave no last_python_err.log behind"
|
||||
assert not (state_dir / "last_python_err.log").exists(), (
|
||||
"successful parse must leave no last_python_err.log behind"
|
||||
)
|
||||
|
||||
|
||||
class TestFailLoudGuard:
|
||||
@@ -214,9 +214,9 @@ class TestFailLoudGuard:
|
||||
tmp_path,
|
||||
)
|
||||
state_dir = tmp_path / ".mempalace" / "hook_state"
|
||||
assert not (
|
||||
state_dir / "last_input.log"
|
||||
).exists(), "unicode-only session_id sanitized to empty must NOT trip the guard"
|
||||
assert not (state_dir / "last_input.log").exists(), (
|
||||
"unicode-only session_id sanitized to empty must NOT trip the guard"
|
||||
)
|
||||
|
||||
@_BOTH_HOOKS
|
||||
def test_literal_unknown_session_id_does_not_trip_guard(self, hook, tmp_path):
|
||||
@@ -231,9 +231,9 @@ class TestFailLoudGuard:
|
||||
tmp_path,
|
||||
)
|
||||
state_dir = tmp_path / ".mempalace" / "hook_state"
|
||||
assert not (
|
||||
state_dir / "last_input.log"
|
||||
).exists(), "literal session_id='unknown' must NOT trip the guard"
|
||||
assert not (state_dir / "last_input.log").exists(), (
|
||||
"literal session_id='unknown' must NOT trip the guard"
|
||||
)
|
||||
|
||||
@_BOTH_HOOKS
|
||||
def test_dump_is_bounded_and_overwritten(self, hook, tmp_path):
|
||||
@@ -251,9 +251,9 @@ class TestFailLoudGuard:
|
||||
big_payload = "x" * 4097
|
||||
_run_hook(hook, big_payload, tmp_path)
|
||||
last_input = tmp_path / ".mempalace" / "hook_state" / "last_input.log"
|
||||
assert (
|
||||
last_input.stat().st_size == 4096
|
||||
), f"cap must be exactly 4096 bytes; got {last_input.stat().st_size}"
|
||||
assert last_input.stat().st_size == 4096, (
|
||||
f"cap must be exactly 4096 bytes; got {last_input.stat().st_size}"
|
||||
)
|
||||
# Second failure with a smaller payload (also not valid JSON, so the
|
||||
# guard fires) overwrites the first; the file shrinks instead of
|
||||
# accumulating.
|
||||
@@ -310,9 +310,9 @@ class TestFailLoudGuard:
|
||||
# Python's json.load raises JSONDecodeError with a recognizable
|
||||
# traceback. Don't pin the exact message (it varies by Python
|
||||
# version) but assert at least one canonical marker is present.
|
||||
assert (
|
||||
"Traceback" in contents or "json" in contents.lower()
|
||||
), f"expected Python traceback or json error, got: {contents!r}"
|
||||
assert "Traceback" in contents or "json" in contents.lower(), (
|
||||
f"expected Python traceback or json error, got: {contents!r}"
|
||||
)
|
||||
|
||||
@_BOTH_HOOKS
|
||||
def test_python_stderr_log_is_not_world_readable_on_failure(self, hook, tmp_path):
|
||||
|
||||
+184
-11
@@ -28,9 +28,44 @@ from mempalace.hooks_cli import (
|
||||
hook_session_start,
|
||||
hook_precompact,
|
||||
run_hook,
|
||||
_claim_mine_slot,
|
||||
_pid_file_for_cmd,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _isolated_existing_palace_root(monkeypatch, tmp_path):
|
||||
"""Give every test an isolated, *existing* PALACE_ROOT/STATE_DIR.
|
||||
|
||||
Regression for #1510: nine save / log / precompact tests assumed
|
||||
``~/.mempalace`` existed and only passed in the full suite because an
|
||||
earlier test file (``test_cli.py``) created it as a side effect, so
|
||||
the ``_palace_root_exists()`` kill-switch was satisfied. Run in
|
||||
isolation they short-circuited and failed.
|
||||
|
||||
Defaulting every test to a per-test palace root that exists makes
|
||||
them robust on their own and protects future tests from the same
|
||||
trap. ``_MINE_PID_DIR`` is patched too: it is derived from
|
||||
``STATE_DIR`` *at module import* (hooks_cli.py:277), so patching
|
||||
``STATE_DIR`` alone would leave mine-spawning tests writing PID files
|
||||
under the import-time location instead of the per-test root. The
|
||||
state dir is created so the docstring's "existing" promise holds.
|
||||
|
||||
Tests that exercise the absent-root kill-switch path call
|
||||
``_redirect_palace_root`` (or set their own PALACE_ROOT) *after* this
|
||||
fixture; ``monkeypatch``'s last-write-wins means they keep their
|
||||
absent/file root and teardown still restores the real module value.
|
||||
"""
|
||||
root = tmp_path / ".mempalace"
|
||||
state_dir = root / "hook_state"
|
||||
state_dir.mkdir(parents=True, exist_ok=True)
|
||||
monkeypatch.setattr(hooks_cli_mod, "PALACE_ROOT", root)
|
||||
monkeypatch.setattr(hooks_cli_mod, "STATE_DIR", state_dir)
|
||||
monkeypatch.setattr(hooks_cli_mod, "_MINE_PID_DIR", state_dir / "mine_pids")
|
||||
monkeypatch.setattr(hooks_cli_mod, "_state_dir_initialized", False)
|
||||
return root
|
||||
|
||||
|
||||
# --- _mempalace_python ---
|
||||
|
||||
|
||||
@@ -650,6 +685,42 @@ def test_mine_sync_uses_mempalace_python(tmp_path):
|
||||
assert cmd[0] == "/fake/venv/python"
|
||||
|
||||
|
||||
def test_claim_mine_slot_writes_live_placeholder_pid(tmp_path):
|
||||
"""Regression #1443: claimed slots must not be empty during spawn startup."""
|
||||
cmd = ["mempalace", "mine", "/tmp/proj", "--mode", "projects"]
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
|
||||
with patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir):
|
||||
pid_file = _claim_mine_slot(cmd)
|
||||
|
||||
assert pid_file == _pid_file_for_cmd(cmd)
|
||||
# Format: "{pid} {unix_timestamp}" — first token must be our PID.
|
||||
content = pid_file.read_text().strip()
|
||||
assert content.split()[0] == str(os.getpid())
|
||||
assert _mine_already_running(cmd) is True
|
||||
assert _claim_mine_slot(cmd) is None
|
||||
|
||||
|
||||
def test_claim_mine_slot_reclaimed_slot_writes_live_placeholder_pid(tmp_path):
|
||||
"""Regression #1443: stale-slot reclaim must also write a live placeholder."""
|
||||
cmd = ["mempalace", "mine", "/tmp/proj", "--mode", "projects"]
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
|
||||
with (
|
||||
patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir),
|
||||
patch("mempalace.hooks_cli._pid_alive", return_value=False),
|
||||
):
|
||||
pid_file = _pid_file_for_cmd(cmd)
|
||||
pid_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
pid_file.write_text("12345")
|
||||
|
||||
reclaimed = _claim_mine_slot(cmd)
|
||||
|
||||
assert reclaimed == pid_file
|
||||
# Format: "{pid} {unix_timestamp}" — first token must be our PID.
|
||||
assert pid_file.read_text().strip().split()[0] == str(os.getpid())
|
||||
|
||||
|
||||
def test_maybe_auto_ingest_ignores_transcript_arg_path(tmp_path):
|
||||
"""_maybe_auto_ingest does NOT mine the transcript directory.
|
||||
|
||||
@@ -721,7 +792,9 @@ def test_maybe_auto_ingest_skips_when_mine_running(tmp_path):
|
||||
]
|
||||
pid_file = _pid_file_for_cmd(cmd)
|
||||
pid_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
pid_file.write_text(str(os.getpid()))
|
||||
import time as _time
|
||||
|
||||
pid_file.write_text(f"{os.getpid()} {int(_time.time())}")
|
||||
with patch("mempalace.hooks_cli._mempalace_python", return_value=sys.executable):
|
||||
with patch("mempalace.hooks_cli.subprocess.Popen") as mock_popen:
|
||||
_maybe_auto_ingest()
|
||||
@@ -787,6 +860,8 @@ def test_spawn_mine_uses_detached_kwargs(tmp_path):
|
||||
|
||||
def test_spawn_mine_skips_when_target_running(tmp_path):
|
||||
"""A second spawn for the same cmd target while the first is alive must skip."""
|
||||
import time as _time
|
||||
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
with patch("mempalace.hooks_cli.STATE_DIR", tmp_path):
|
||||
with patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir):
|
||||
@@ -795,7 +870,7 @@ def test_spawn_mine_skips_when_target_running(tmp_path):
|
||||
cmd = ["mempalace", "mine", "/tmp/proj", "--mode", "projects"]
|
||||
pid_file = _pid_file_for_cmd(cmd)
|
||||
pid_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
pid_file.write_text(str(os.getpid())) # live PID
|
||||
pid_file.write_text(f"{os.getpid()} {int(_time.time())}") # live PID, fresh
|
||||
|
||||
with patch("mempalace.hooks_cli.subprocess.Popen") as mock_popen:
|
||||
_spawn_mine(cmd)
|
||||
@@ -833,8 +908,9 @@ def test_spawn_mine_reclaims_stale_slot(tmp_path):
|
||||
mock_popen.return_value.pid = 4242
|
||||
_spawn_mine(cmd)
|
||||
mock_popen.assert_called_once()
|
||||
# New PID is recorded in the reclaimed slot.
|
||||
assert pid_file.read_text().strip() == "4242"
|
||||
# New PID is recorded in the reclaimed slot (format: "{pid} {timestamp}").
|
||||
content = pid_file.read_text().strip()
|
||||
assert content.split()[0] == "4242"
|
||||
|
||||
|
||||
def test_spawn_mine_releases_slot_on_oserror(tmp_path):
|
||||
@@ -850,9 +926,9 @@ def test_spawn_mine_releases_slot_on_oserror(tmp_path):
|
||||
with patch("mempalace.hooks_cli.subprocess.Popen", side_effect=OSError("spawn fail")):
|
||||
with pytest.raises(OSError):
|
||||
_spawn_mine(cmd)
|
||||
assert (
|
||||
not pid_file.exists()
|
||||
), "slot must be released so the next hook fire isn't permanently blocked"
|
||||
assert not pid_file.exists(), (
|
||||
"slot must be released so the next hook fire isn't permanently blocked"
|
||||
)
|
||||
|
||||
|
||||
def test_spawn_mine_passes_pid_file_env_var(tmp_path):
|
||||
@@ -910,7 +986,9 @@ def test_ingest_transcript_skips_when_target_running(tmp_path):
|
||||
]
|
||||
pid_file = _pid_file_for_cmd(expected_cmd)
|
||||
pid_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
pid_file.write_text(str(os.getpid())) # live target
|
||||
import time as _time
|
||||
|
||||
pid_file.write_text(f"{os.getpid()} {int(_time.time())}") # live target, fresh
|
||||
|
||||
with patch("mempalace.hooks_cli.subprocess.Popen") as mock_popen:
|
||||
_ingest_transcript(str(transcript))
|
||||
@@ -948,14 +1026,106 @@ def test_mine_already_running_dead_pid(tmp_path):
|
||||
|
||||
|
||||
def test_mine_already_running_live_pid(tmp_path):
|
||||
"""Returns True when the slot's recorded PID is alive."""
|
||||
"""Returns True when the slot's recorded PID is alive (new {pid ts} format)."""
|
||||
import time as _time
|
||||
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd = ["mempalace", "mine", "/tmp/x", "--mode", "projects"]
|
||||
_seed_slot(pid_dir, cmd, str(os.getpid())) # current process is alive
|
||||
# Use a recent timestamp so the default 2 h timeout does not trigger.
|
||||
_seed_slot(pid_dir, cmd, f"{os.getpid()} {int(_time.time())}")
|
||||
with patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir):
|
||||
assert _mine_already_running(cmd) is True
|
||||
|
||||
|
||||
def test_mine_already_running_live_pid_bare_format(tmp_path):
|
||||
"""Old bare-PID format uses file mtime for the stale-by-age check."""
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd = ["mempalace", "mine", "/tmp/x", "--mode", "projects"]
|
||||
_seed_slot(pid_dir, cmd, str(os.getpid())) # old format: bare PID
|
||||
with patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir):
|
||||
assert _mine_already_running(cmd) is True
|
||||
|
||||
|
||||
def test_mine_already_running_bare_pid_old_mtime_is_stale(tmp_path):
|
||||
"""Old bare-PID slots are reclaimed once their file mtime exceeds timeout."""
|
||||
import time as _time
|
||||
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd = ["mempalace", "mine", "/tmp/x", "--mode", "projects"]
|
||||
slot = _seed_slot(pid_dir, cmd, str(os.getpid()))
|
||||
old_mtime = _time.time() - 3601
|
||||
os.utime(slot, (old_mtime, old_mtime))
|
||||
with (
|
||||
patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir),
|
||||
patch.dict("os.environ", {"MEMPALACE_MINE_TIMEOUT_HOURS": "1"}),
|
||||
):
|
||||
assert _mine_already_running(cmd) is False
|
||||
|
||||
|
||||
def test_mine_already_running_malformed_timestamp_is_stale(tmp_path):
|
||||
"""Malformed timestamps fail soft instead of crashing hook execution."""
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd = ["mempalace", "mine", "/tmp/x", "--mode", "projects"]
|
||||
_seed_slot(pid_dir, cmd, f"{os.getpid()} not-a-timestamp")
|
||||
with patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir):
|
||||
assert _mine_already_running(cmd) is False
|
||||
|
||||
|
||||
def test_mine_slot_timeout_invalid_env_disables_timeout():
|
||||
"""Invalid MEMPALACE_MINE_TIMEOUT_HOURS disables stale-by-age checks."""
|
||||
from mempalace.hooks_cli import _mine_slot_timeout_secs
|
||||
|
||||
with patch.dict("os.environ", {"MEMPALACE_MINE_TIMEOUT_HOURS": "nope"}):
|
||||
assert _mine_slot_timeout_secs() == 0.0
|
||||
|
||||
|
||||
def test_mine_already_running_live_pid_exceeds_timeout(tmp_path):
|
||||
"""Returns False when PID is alive but has exceeded the configured timeout (#1552)."""
|
||||
import time as _time
|
||||
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd = ["mempalace", "mine", "/tmp/x", "--mode", "projects"]
|
||||
# Timestamp far in the past so any positive timeout fires immediately.
|
||||
old_ts = int(_time.time()) - 3601 # 1 second past 1-hour mark
|
||||
_seed_slot(pid_dir, cmd, f"{os.getpid()} {old_ts}")
|
||||
with (
|
||||
patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir),
|
||||
patch.dict("os.environ", {"MEMPALACE_MINE_TIMEOUT_HOURS": "1"}),
|
||||
):
|
||||
assert _mine_already_running(cmd) is False
|
||||
|
||||
|
||||
def test_mine_already_running_live_pid_within_timeout(tmp_path):
|
||||
"""Returns True when PID is alive and has NOT exceeded the configured timeout."""
|
||||
import time as _time
|
||||
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd = ["mempalace", "mine", "/tmp/x", "--mode", "projects"]
|
||||
recent_ts = int(_time.time()) - 60 # only 1 minute old
|
||||
_seed_slot(pid_dir, cmd, f"{os.getpid()} {recent_ts}")
|
||||
with (
|
||||
patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir),
|
||||
patch.dict("os.environ", {"MEMPALACE_MINE_TIMEOUT_HOURS": "2"}),
|
||||
):
|
||||
assert _mine_already_running(cmd) is True
|
||||
|
||||
|
||||
def test_mine_already_running_timeout_zero_disables_check(tmp_path):
|
||||
"""MEMPALACE_MINE_TIMEOUT_HOURS=0 disables the age-based stale check."""
|
||||
import time as _time
|
||||
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd = ["mempalace", "mine", "/tmp/x", "--mode", "projects"]
|
||||
old_ts = int(_time.time()) - 86400 # 24 hours ago — stale under any non-zero timeout
|
||||
_seed_slot(pid_dir, cmd, f"{os.getpid()} {old_ts}")
|
||||
with (
|
||||
patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir),
|
||||
patch.dict("os.environ", {"MEMPALACE_MINE_TIMEOUT_HOURS": "0"}),
|
||||
):
|
||||
# Timeout disabled — alive PID is always considered running.
|
||||
assert _mine_already_running(cmd) is True
|
||||
|
||||
|
||||
def test_mine_already_running_corrupt_file(tmp_path):
|
||||
"""Returns False when the slot contains non-integer content."""
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
@@ -967,10 +1137,13 @@ def test_mine_already_running_corrupt_file(tmp_path):
|
||||
|
||||
def test_mine_already_running_distinct_cmds_independent(tmp_path):
|
||||
"""Slots are keyed per cmd; an alive entry for cmd A doesn't shadow cmd B."""
|
||||
import time as _time
|
||||
|
||||
pid_dir = tmp_path / "mine_pids"
|
||||
cmd_a = ["mempalace", "mine", "/tmp/a", "--mode", "projects"]
|
||||
cmd_b = ["mempalace", "mine", "/tmp/b", "--mode", "projects"]
|
||||
_seed_slot(pid_dir, cmd_a, str(os.getpid()))
|
||||
recent_ts = int(_time.time())
|
||||
_seed_slot(pid_dir, cmd_a, f"{os.getpid()} {recent_ts}")
|
||||
with patch("mempalace.hooks_cli._MINE_PID_DIR", pid_dir):
|
||||
assert _mine_already_running(cmd_a) is True
|
||||
assert _mine_already_running(cmd_b) is False
|
||||
|
||||
+12
-12
@@ -123,13 +123,13 @@ class TestMempalPythonOverride:
|
||||
{"session_id": "abc", "stop_hook_active": False, "transcript_path": ""},
|
||||
env_overrides={"MEMPAL_PYTHON": str(fake), "HOME": str(tmp_path)},
|
||||
)
|
||||
assert (
|
||||
result.returncode == 0
|
||||
), f"hook exited non-zero: stderr={result.stderr!r} stdout={result.stdout!r}"
|
||||
assert result.returncode == 0, (
|
||||
f"hook exited non-zero: stderr={result.stderr!r} stdout={result.stdout!r}"
|
||||
)
|
||||
invocations = marker.read_text().splitlines() if marker.exists() else []
|
||||
assert (
|
||||
"override_python" in invocations
|
||||
), f"MEMPAL_PYTHON override was not used. Marker log: {invocations!r}"
|
||||
assert "override_python" in invocations, (
|
||||
f"MEMPAL_PYTHON override was not used. Marker log: {invocations!r}"
|
||||
)
|
||||
|
||||
def test_ignores_override_when_not_executable(self, tmp_path):
|
||||
"""If MEMPAL_PYTHON is set but the file isn't executable, the
|
||||
@@ -143,9 +143,9 @@ class TestMempalPythonOverride:
|
||||
{"session_id": "abc", "stop_hook_active": False, "transcript_path": ""},
|
||||
env_overrides={"MEMPAL_PYTHON": str(bogus), "HOME": str(tmp_path)},
|
||||
)
|
||||
assert (
|
||||
result.returncode == 0
|
||||
), f"hook crashed on non-executable MEMPAL_PYTHON: {result.stderr!r}"
|
||||
assert result.returncode == 0, (
|
||||
f"hook crashed on non-executable MEMPAL_PYTHON: {result.stderr!r}"
|
||||
)
|
||||
|
||||
def test_falls_back_to_path_when_unset(self, tmp_path):
|
||||
"""With MEMPAL_PYTHON unset, the hook uses whatever ``python3``
|
||||
@@ -165,6 +165,6 @@ class TestMempalPythonOverride:
|
||||
)
|
||||
assert result.returncode == 0
|
||||
invocations = marker.read_text().splitlines() if marker.exists() else []
|
||||
assert (
|
||||
"python3" in invocations
|
||||
), f"fallback-to-PATH did not use the shimmed python3. Marker log: {invocations!r}"
|
||||
assert "python3" in invocations, (
|
||||
f"fallback-to-PATH did not use the shimmed python3. Marker log: {invocations!r}"
|
||||
)
|
||||
|
||||
@@ -79,8 +79,7 @@ class TestCandidateUnion:
|
||||
result = search_memories(_NARRATIVE_QUERY, palace, n_results=5, candidate_strategy="union")
|
||||
ids = [h["source_file"] for h in result["results"]]
|
||||
assert "brand_voice_D4.md" in ids, (
|
||||
"union mode must surface BM25-strong docs even when vector signal "
|
||||
f"is weak; got {ids}"
|
||||
f"union mode must surface BM25-strong docs even when vector signal is weak; got {ids}"
|
||||
)
|
||||
|
||||
def test_union_preserves_vector_hits(self, tmp_path):
|
||||
@@ -138,9 +137,9 @@ class TestCandidateUnion:
|
||||
# 4-doc corpus, n_results=2 → union pool can grow to ~8 candidates,
|
||||
# rerank reorders them, but final list must respect the cap.
|
||||
result = search_memories(_NARRATIVE_QUERY, palace, n_results=2, candidate_strategy="union")
|
||||
assert (
|
||||
len(result["results"]) <= 2
|
||||
), f"union must trim to n_results=2; got {len(result['results'])} results"
|
||||
assert len(result["results"]) <= 2, (
|
||||
f"union must trim to n_results=2; got {len(result['results'])} results"
|
||||
)
|
||||
|
||||
def test_union_skipped_when_max_distance_set(self, tmp_path):
|
||||
"""``max_distance`` is a vector-distance threshold; BM25-only
|
||||
|
||||
+3
-3
@@ -155,7 +155,7 @@ def test_direct_address_key_is_singular_string_for_all_locales():
|
||||
)
|
||||
if "direct_address_pattern" in section:
|
||||
val = section["direct_address_pattern"]
|
||||
assert isinstance(
|
||||
val, str
|
||||
), f"{lang}: 'direct_address_pattern' must be str, got {type(val).__name__}"
|
||||
assert isinstance(val, str), (
|
||||
f"{lang}: 'direct_address_pattern' must be str, got {type(val).__name__}"
|
||||
)
|
||||
assert val, f"{lang}: 'direct_address_pattern' is empty"
|
||||
|
||||
@@ -74,9 +74,9 @@ def test_get_entity_patterns_shares_cache_across_cases():
|
||||
cache_keys = list(i18n._entity_cache.keys())
|
||||
get_entity_patterns(("ZH-CN",))
|
||||
get_entity_patterns(("zh-cn",))
|
||||
assert len(i18n._entity_cache) == len(
|
||||
cache_keys
|
||||
), "different casings of the same language must not create new cache entries"
|
||||
assert len(i18n._entity_cache) == len(cache_keys), (
|
||||
"different casings of the same language must not create new cache entries"
|
||||
)
|
||||
|
||||
|
||||
def test_unknown_language_still_falls_back_to_english():
|
||||
|
||||
+7
-7
@@ -65,15 +65,15 @@ def test_init_filters_sys_path_from_leaked_pythonpath(pythonpath):
|
||||
out = result.stdout
|
||||
# Env must be preserved verbatim: embedded callers may need it.
|
||||
expected_env = repr(pythonpath) if pythonpath is not None else repr(None)
|
||||
assert (
|
||||
f"ENV: {expected_env}" in out
|
||||
), f"PYTHONPATH should be preserved by package import: {diag}"
|
||||
assert f"ENV: {expected_env}" in out, (
|
||||
f"PYTHONPATH should be preserved by package import: {diag}"
|
||||
)
|
||||
assert "SENTINEL_IN_PATH: False" in out, f"sentinel-prefix leak: {diag}"
|
||||
# Filter must not over-strip: the mempalace package itself must remain
|
||||
# importable, so its parent directory must survive on sys.path.
|
||||
assert (
|
||||
"MEMPALACE_PARENT_PRESENT: True" in out
|
||||
), f"filter over-stripped sys.path (mempalace parent gone): {diag}"
|
||||
assert "MEMPALACE_PARENT_PRESENT: True" in out, (
|
||||
f"filter over-stripped sys.path (mempalace parent gone): {diag}"
|
||||
)
|
||||
|
||||
|
||||
def test_init_preserves_cwd_marker_when_pythonpath_collides():
|
||||
@@ -94,7 +94,7 @@ def test_init_preserves_cwd_marker_when_pythonpath_collides():
|
||||
text=True,
|
||||
check=False,
|
||||
)
|
||||
diag = f"rc={result.returncode}; stdout={result.stdout!r}; " f"stderr={result.stderr!r}"
|
||||
diag = f"rc={result.returncode}; stdout={result.stdout!r}; stderr={result.stderr!r}"
|
||||
assert result.returncode == 0, f"subprocess failed: {diag}"
|
||||
assert "CWD_IN_PATH: True" in result.stdout, f"cwd marker dropped: {diag}"
|
||||
assert "DOT_IN_PATH: False" in result.stdout, f"dot leak survived: {diag}"
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
"""Tests for virtual line numbering (mempalace 3.3.6, integrated in PR #1555).
|
||||
|
||||
Run with:
|
||||
pytest tests/test_line_numbers.py -v
|
||||
"""
|
||||
|
||||
from mempalace.searcher import ( # noqa: E402
|
||||
extract_line_range,
|
||||
render_with_line_numbers,
|
||||
)
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# render_with_line_numbers
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_render_empty_string():
|
||||
assert render_with_line_numbers("") == ""
|
||||
|
||||
|
||||
def test_render_single_line():
|
||||
assert render_with_line_numbers("hello") == "[1] hello"
|
||||
|
||||
|
||||
def test_render_multi_line():
|
||||
text = "alpha\nbeta\ngamma"
|
||||
expected = "[1] alpha\n[2] beta\n[3] gamma"
|
||||
assert render_with_line_numbers(text) == expected
|
||||
|
||||
|
||||
def test_render_custom_start_line():
|
||||
text = "first\nsecond"
|
||||
expected = "[5] first\n[6] second"
|
||||
assert render_with_line_numbers(text, start_line=5) == expected
|
||||
|
||||
|
||||
def test_render_preserves_already_numbered_lines():
|
||||
"""Lines that already start with [N] must pass through unchanged."""
|
||||
text = "[1] already numbered\n[2] also numbered"
|
||||
assert render_with_line_numbers(text) == text
|
||||
|
||||
|
||||
def test_render_preserves_already_numbered_with_offset():
|
||||
"""Already-numbered lines pass through verbatim regardless of start_line."""
|
||||
text = "[42] keep this number\n[43] and this"
|
||||
assert render_with_line_numbers(text, start_line=100) == text
|
||||
|
||||
|
||||
def test_render_mixed_numbered_and_plain():
|
||||
"""Counter advances on every line; numbered lines pass through, plain lines get the running counter."""
|
||||
text = "[10] kept\nplain line\n[12] also kept"
|
||||
expected = "[10] kept\n[2] plain line\n[12] also kept"
|
||||
assert render_with_line_numbers(text) == expected
|
||||
|
||||
|
||||
def test_render_preserves_blank_lines():
|
||||
"""Blank lines must still get a number — they're real positions in the drawer."""
|
||||
text = "first\n\nthird"
|
||||
expected = "[1] first\n[2] \n[3] third"
|
||||
assert render_with_line_numbers(text) == expected
|
||||
|
||||
|
||||
def test_render_preserves_trailing_newline_semantics():
|
||||
"""Splitting on \\n and rejoining preserves the original boundary count."""
|
||||
text = "a\nb\n"
|
||||
result = render_with_line_numbers(text)
|
||||
# "a\nb\n".split("\n") → ["a", "b", ""] — three positions
|
||||
assert result == "[1] a\n[2] b\n[3] "
|
||||
|
||||
|
||||
def test_render_none_input_returns_empty():
|
||||
"""Defensive: None must not crash."""
|
||||
assert render_with_line_numbers(None) == ""
|
||||
|
||||
|
||||
def test_render_does_not_modify_original_text():
|
||||
"""Function is pure — no mutation of input."""
|
||||
original = "line one\nline two"
|
||||
snapshot = original
|
||||
render_with_line_numbers(original)
|
||||
assert original == snapshot
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# extract_line_range
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_extract_single_line():
|
||||
text = "a\nb\nc\nd\ne"
|
||||
assert extract_line_range(text, 3, 3) == "[3] c"
|
||||
|
||||
|
||||
def test_extract_inclusive_range():
|
||||
text = "a\nb\nc\nd\ne"
|
||||
expected = "[2] b\n[3] c\n[4] d"
|
||||
assert extract_line_range(text, 2, 4) == expected
|
||||
|
||||
|
||||
def test_extract_full_range():
|
||||
text = "first\nsecond\nthird"
|
||||
expected = "[1] first\n[2] second\n[3] third"
|
||||
assert extract_line_range(text, 1, 3) == expected
|
||||
|
||||
|
||||
def test_extract_end_beyond_length_clips():
|
||||
"""If line_end exceeds the document, return what's available — don't error."""
|
||||
text = "a\nb\nc"
|
||||
expected = "[2] b\n[3] c"
|
||||
assert extract_line_range(text, 2, 99) == expected
|
||||
|
||||
|
||||
def test_extract_start_below_one_clamps():
|
||||
"""start_line < 1 clamps to 1; numbering starts from where extraction starts."""
|
||||
text = "a\nb\nc"
|
||||
# Clamping to 1 means we extract from line 1; numbering starts at 1.
|
||||
expected = "[1] a\n[2] b"
|
||||
assert extract_line_range(text, 0, 2) == expected
|
||||
|
||||
|
||||
def test_extract_start_after_end_returns_empty():
|
||||
text = "a\nb\nc"
|
||||
assert extract_line_range(text, 5, 2) == ""
|
||||
|
||||
|
||||
def test_extract_empty_text():
|
||||
assert extract_line_range("", 1, 5) == ""
|
||||
|
||||
|
||||
def test_extract_honors_already_numbered_lines():
|
||||
"""If the slice contains already-numbered lines, they pass through."""
|
||||
text = "plain\n[42] numbered\nplain again"
|
||||
expected = "[1] plain\n[42] numbered\n[3] plain again"
|
||||
assert extract_line_range(text, 1, 3) == expected
|
||||
|
||||
|
||||
def test_extract_uses_drawer_line_numbers_not_relative():
|
||||
"""When extracting lines 5-7, the rendered numbers must be [5][6][7], not [1][2][3].
|
||||
|
||||
This is the closet-pointer contract: a pointer →2026-01-18:L55-L72 must show
|
||||
[55] through [72] in the rendered output, so the user sees which drawer
|
||||
positions they're reading.
|
||||
"""
|
||||
text = "\n".join(f"line{i}" for i in range(1, 11)) # line1..line10
|
||||
result = extract_line_range(text, 5, 7)
|
||||
assert result.startswith("[5] line5")
|
||||
assert "[6] line6" in result
|
||||
assert result.endswith("[7] line7")
|
||||
assert "[1]" not in result
|
||||
assert "[8]" not in result
|
||||
|
||||
|
||||
def test_extract_does_not_modify_original_text():
|
||||
original = "x\ny\nz"
|
||||
snapshot = original
|
||||
extract_line_range(original, 1, 2)
|
||||
assert original == snapshot
|
||||
+12
-12
@@ -445,9 +445,9 @@ def test_openai_compat_api_key_source_flag_when_explicit(monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "sk-from-env-irrelevant")
|
||||
p = OpenAICompatProvider(model="x", endpoint="http://h", api_key="sk-from-flag")
|
||||
assert p.api_key == "sk-from-flag"
|
||||
assert (
|
||||
p.api_key_source == "flag"
|
||||
), f"Explicit api_key arg must produce api_key_source='flag'; got {p.api_key_source!r}"
|
||||
assert p.api_key_source == "flag", (
|
||||
f"Explicit api_key arg must produce api_key_source='flag'; got {p.api_key_source!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_openai_compat_api_key_source_env_when_fallback(monkeypatch):
|
||||
@@ -458,9 +458,9 @@ def test_openai_compat_api_key_source_env_when_fallback(monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "sk-from-env")
|
||||
p = OpenAICompatProvider(model="x", endpoint="http://h")
|
||||
assert p.api_key == "sk-from-env"
|
||||
assert (
|
||||
p.api_key_source == "env"
|
||||
), f"Env-fallback api_key must produce api_key_source='env'; got {p.api_key_source!r}"
|
||||
assert p.api_key_source == "env", (
|
||||
f"Env-fallback api_key must produce api_key_source='env'; got {p.api_key_source!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_anthropic_api_key_source_tracking(monkeypatch):
|
||||
@@ -468,14 +468,14 @@ def test_anthropic_api_key_source_tracking(monkeypatch):
|
||||
passed explicitly, 'env' when resolved from ANTHROPIC_API_KEY env."""
|
||||
monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-env")
|
||||
p_flag = AnthropicProvider(model="claude-haiku", api_key="sk-ant-flag")
|
||||
assert (
|
||||
p_flag.api_key_source == "flag"
|
||||
), f"Explicit api_key must produce 'flag'; got {p_flag.api_key_source!r}"
|
||||
assert p_flag.api_key_source == "flag", (
|
||||
f"Explicit api_key must produce 'flag'; got {p_flag.api_key_source!r}"
|
||||
)
|
||||
p_env = AnthropicProvider(model="claude-haiku")
|
||||
assert p_env.api_key == "sk-ant-env"
|
||||
assert (
|
||||
p_env.api_key_source == "env"
|
||||
), f"Env-fallback must produce 'env'; got {p_env.api_key_source!r}"
|
||||
assert p_env.api_key_source == "env", (
|
||||
f"Env-fallback must produce 'env'; got {p_env.api_key_source!r}"
|
||||
)
|
||||
|
||||
|
||||
def test_ollama_api_key_source_is_none():
|
||||
|
||||
+387
-22
@@ -63,12 +63,12 @@ def test_mcp_main_strips_leaked_pythonpath_from_env():
|
||||
)
|
||||
diag = f"rc={result.returncode}; stdout={result.stdout!r}; stderr={result.stderr!r}"
|
||||
assert result.returncode == 0, f"subprocess failed: {diag}"
|
||||
assert (
|
||||
f"ENV_MID: {expected_env!r}" in result.stderr
|
||||
), f"package import unexpectedly stripped env (regression in __init__.py): {diag}"
|
||||
assert (
|
||||
"SENTINEL_IN_PATH: False" in result.stderr
|
||||
), f"package import did not filter sys.path (regression in __init__.py): {diag}"
|
||||
assert f"ENV_MID: {expected_env!r}" in result.stderr, (
|
||||
f"package import unexpectedly stripped env (regression in __init__.py): {diag}"
|
||||
)
|
||||
assert "SENTINEL_IN_PATH: False" in result.stderr, (
|
||||
f"package import did not filter sys.path (regression in __init__.py): {diag}"
|
||||
)
|
||||
assert "ENV_AFTER: None" in result.stderr, f"MCP server did not strip PYTHONPATH: {diag}"
|
||||
|
||||
|
||||
@@ -250,9 +250,9 @@ class TestColdStartDiagnostics:
|
||||
)
|
||||
assert result.returncode == 0, f"stderr={result.stderr!r}"
|
||||
assert not marker.exists(), f"warmup ran for explicit-falsy value {value!r}"
|
||||
assert (
|
||||
"not recognized" not in result.stderr
|
||||
), f"explicit-falsy {value!r} should not log a warning; stderr={result.stderr!r}"
|
||||
assert "not recognized" not in result.stderr, (
|
||||
f"explicit-falsy {value!r} should not log a warning; stderr={result.stderr!r}"
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize("value", ["tru", "maybe", "ENABLED", "2"])
|
||||
def test_eager_warmup_unrecognized_value_warns_and_skips_collection_open(self, tmp_path, value):
|
||||
@@ -298,9 +298,9 @@ class TestColdStartDiagnostics:
|
||||
extra_code=extra,
|
||||
)
|
||||
assert result.returncode == 0, f"stderr={result.stderr!r}"
|
||||
assert (
|
||||
open_marker.exists()
|
||||
), f"_get_collection not called for {value!r}; stderr={result.stderr!r}"
|
||||
assert open_marker.exists(), (
|
||||
f"_get_collection not called for {value!r}; stderr={result.stderr!r}"
|
||||
)
|
||||
assert query_marker.exists(), (
|
||||
f"col.query not invoked for {value!r} — warmup is a no-op "
|
||||
f"(would let cold-load hit first MCP call); stderr={result.stderr!r}"
|
||||
@@ -1134,9 +1134,9 @@ class TestWriteTools:
|
||||
|
||||
assert result1["success"] is True
|
||||
assert result2["success"] is True
|
||||
assert (
|
||||
result1["drawer_id"] != result2["drawer_id"]
|
||||
), "Documents with shared header but different content must have distinct drawer IDs"
|
||||
assert result1["drawer_id"] != result2["drawer_id"], (
|
||||
"Documents with shared header but different content must have distinct drawer IDs"
|
||||
)
|
||||
|
||||
def test_delete_drawer(self, monkeypatch, config, palace_path, seeded_collection, kg):
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
@@ -1371,7 +1371,14 @@ class TestWriteTools:
|
||||
verbatim, and both hyphen and underscore queries find the result."""
|
||||
from mempalace import mcp_server, palace_graph
|
||||
|
||||
monkeypatch.setattr(palace_graph, "_TUNNEL_FILE", str(tmp_path / "tunnels.json"))
|
||||
tunnel_file = tmp_path / "tunnels.json"
|
||||
monkeypatch.setattr(palace_graph, "_get_tunnel_file", lambda *a, **kw: str(tunnel_file))
|
||||
monkeypatch.setattr(
|
||||
palace_graph,
|
||||
"_legacy_tunnel_file",
|
||||
lambda: str(tmp_path / "legacy-tunnels.json"),
|
||||
)
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda *a, **kw: None)
|
||||
|
||||
t = mcp_server.tool_create_tunnel(
|
||||
source_wing="other-wing",
|
||||
@@ -1386,6 +1393,180 @@ class TestWriteTools:
|
||||
assert len(mcp_server.tool_list_tunnels(wing="my-wing")) == 1
|
||||
assert len(mcp_server.tool_list_tunnels(wing="my_wing")) == 1
|
||||
|
||||
def test_tool_create_tunnel_surfaces_value_error(self, monkeypatch):
|
||||
"""Regression for #1473: a ValueError from create_tunnel (e.g. a
|
||||
missing room) must be returned to the caller as a clear error,
|
||||
not escape and get wrapped as the opaque 'Internal tool error'."""
|
||||
from mempalace import mcp_server
|
||||
|
||||
msg = "Target room 'does-not-exist-probe' does not exist in wing 'wing_minerva'"
|
||||
|
||||
def _raise(*args, **kwargs):
|
||||
raise ValueError(msg)
|
||||
|
||||
monkeypatch.setattr(mcp_server, "create_tunnel", _raise)
|
||||
|
||||
result = mcp_server.tool_create_tunnel(
|
||||
source_wing="wing_minerva",
|
||||
source_room="fx-invariants",
|
||||
target_wing="wing_minerva",
|
||||
target_room="does-not-exist-probe",
|
||||
)
|
||||
|
||||
assert result == {"error": msg}
|
||||
|
||||
def test_add_drawer_normal_content_single_drawer(self, monkeypatch, config, palace_path, kg):
|
||||
"""Regression catch: content below CHUNK_SIZE produces exactly
|
||||
one drawer with ``chunks == 1``. Pre-#1539 contract preserved."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_add_drawer
|
||||
|
||||
result = tool_add_drawer(wing="w", room="r", content="Short content well under chunk_size.")
|
||||
assert result["success"] is True
|
||||
assert result["chunks"] == 1
|
||||
assert "chunk_ids" not in result
|
||||
_client2, col = _get_collection(palace_path)
|
||||
del _client2
|
||||
assert col.count() == 1
|
||||
assert col.get()["ids"] == [result["drawer_id"]]
|
||||
|
||||
def test_add_drawer_oversized_content_chunked(self, monkeypatch, config, palace_path, kg):
|
||||
"""Regression for #1539: content far above chunk_size must be
|
||||
sliced into bounded per-chunk drawers, each linked by a
|
||||
``parent_drawer_id`` metadata field. No stored document may
|
||||
exceed the configured chunk_size."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_add_drawer
|
||||
|
||||
oversized = "X" * 10000
|
||||
result = tool_add_drawer(wing="w", room="r", content=oversized)
|
||||
assert result["success"] is True
|
||||
assert result["chunks"] > 1
|
||||
assert "chunk_ids" in result and len(result["chunk_ids"]) == result["chunks"]
|
||||
|
||||
_client2, col = _get_collection(palace_path)
|
||||
del _client2
|
||||
stored = col.get()
|
||||
max_doc = max(len(d) for d in stored["documents"])
|
||||
assert max_doc <= config.chunk_size, (
|
||||
f"no stored document may exceed chunk_size={config.chunk_size}; got max={max_doc}"
|
||||
)
|
||||
# Chroma does not guarantee insertion order on a bare ``get()``;
|
||||
# sort by ``chunk_index`` before joining so the verbatim check
|
||||
# is deterministic.
|
||||
ordered = sorted(
|
||||
zip(stored["metadatas"], stored["documents"]),
|
||||
key=lambda pair: pair[0]["chunk_index"],
|
||||
)
|
||||
assert "".join(doc for _meta, doc in ordered) == oversized
|
||||
parent_ids = {m.get("parent_drawer_id") for m in stored["metadatas"]}
|
||||
assert parent_ids == {result["drawer_id"]}, (
|
||||
f"all chunks must share one parent_drawer_id; got {parent_ids}"
|
||||
)
|
||||
|
||||
def test_add_drawer_oversized_idempotency_skips_duplicate_chunk_writes(
|
||||
self, monkeypatch, config, palace_path, kg
|
||||
):
|
||||
"""Re-calling with identical oversized content must not duplicate
|
||||
any drawer. Idempotency on the chunked path probes the last
|
||||
chunk id (its presence implies the whole batch committed) and
|
||||
also the legacy logical drawer_id so a pre-#1539 single-row
|
||||
write under the same logical id does not get co-resident chunk
|
||||
siblings on the next call."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_add_drawer
|
||||
|
||||
oversized = "Y" * 5000
|
||||
r1 = tool_add_drawer(wing="w", room="r", content=oversized)
|
||||
assert r1["success"] is True and r1["chunks"] > 1
|
||||
r2 = tool_add_drawer(wing="w", room="r", content=oversized)
|
||||
assert r2["success"] is True
|
||||
assert r2.get("reason") == "already_exists"
|
||||
|
||||
_client2, col = _get_collection(palace_path)
|
||||
del _client2
|
||||
assert col.count() == r1["chunks"]
|
||||
# The probe must succeed against the last chunk id (atomicity
|
||||
# signal), and no row must be stored under the logical id.
|
||||
last_chunk = r1["chunk_ids"][-1]
|
||||
assert col.get(ids=[last_chunk])["ids"] == [last_chunk]
|
||||
assert col.get(ids=[r1["drawer_id"]])["ids"] == []
|
||||
|
||||
def test_add_drawer_chunk_metadata_carries_parent_link(
|
||||
self, monkeypatch, config, palace_path, kg
|
||||
):
|
||||
"""Every chunk produced from oversized content must carry both
|
||||
``chunk_index`` (0..N-1) and ``parent_drawer_id`` matching the
|
||||
logical group handle returned to the caller."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_add_drawer
|
||||
|
||||
result = tool_add_drawer(wing="w", room="r", content="Q" * 3500)
|
||||
assert result["success"] is True and result["chunks"] > 1
|
||||
|
||||
_client2, col = _get_collection(palace_path)
|
||||
del _client2
|
||||
stored = col.get()
|
||||
indices = sorted(m["chunk_index"] for m in stored["metadatas"])
|
||||
assert indices == list(range(len(indices)))
|
||||
for meta in stored["metadatas"]:
|
||||
assert meta.get("parent_drawer_id") == result["drawer_id"]
|
||||
|
||||
def test_add_drawer_boundary_exact_chunk_size_stays_single(
|
||||
self, monkeypatch, config, palace_path, kg
|
||||
):
|
||||
"""The ``<= chunk_size`` predicate must include the boundary:
|
||||
content of exactly chunk_size chars stays a single drawer, not
|
||||
an off-by-one chunked write."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_add_drawer
|
||||
|
||||
boundary = "Z" * config.chunk_size
|
||||
result = tool_add_drawer(wing="w", room="r", content=boundary)
|
||||
assert result["success"] is True
|
||||
assert result["chunks"] == 1
|
||||
assert "chunk_ids" not in result
|
||||
|
||||
def test_add_drawer_chunked_logical_id_not_fetchable_directly(
|
||||
self, monkeypatch, config, palace_path, kg
|
||||
):
|
||||
"""Documented contract on the chunked path: ``tool_get_drawer``
|
||||
and ``tool_delete_drawer`` against the returned logical
|
||||
``drawer_id`` report ``not found`` because no row is stored
|
||||
under that id. Callers must iterate ``chunk_ids`` or query by
|
||||
``parent_drawer_id`` metadata."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_add_drawer, tool_delete_drawer, tool_get_drawer
|
||||
|
||||
result = tool_add_drawer(wing="w", room="r", content="P" * 4000)
|
||||
assert result["success"] is True and result["chunks"] > 1
|
||||
|
||||
# tool_get_drawer against logical id: not found.
|
||||
got_logical = tool_get_drawer(result["drawer_id"])
|
||||
assert "error" in got_logical and "not found" in got_logical["error"].lower()
|
||||
|
||||
# tool_get_drawer against the first chunk id: found, full content slice.
|
||||
got_chunk = tool_get_drawer(result["chunk_ids"][0])
|
||||
assert got_chunk["content"] == "P" * config.chunk_size
|
||||
assert got_chunk["metadata"]["parent_drawer_id"] == result["drawer_id"]
|
||||
|
||||
# tool_delete_drawer against logical id: also not found.
|
||||
deleted_logical = tool_delete_drawer(result["drawer_id"])
|
||||
assert deleted_logical["success"] is False
|
||||
assert "not found" in deleted_logical["error"].lower()
|
||||
|
||||
|
||||
# ── KG Tools ────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1916,6 +2097,81 @@ class TestDiaryTools:
|
||||
assert w1["agent"] == "claude"
|
||||
assert w2["agent"] == "claude"
|
||||
|
||||
# ── #1539: oversized-entry chunking ────────────────────────────
|
||||
|
||||
def test_diary_write_normal_entry_single_drawer(self, monkeypatch, config, palace_path, kg):
|
||||
"""Regression catch: a normal entry (< CHUNK_SIZE) must produce
|
||||
exactly one drawer with ``chunks == 1`` in the result. Existing
|
||||
pre-#1539 behaviour preserved for the common path."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_diary_write
|
||||
|
||||
r = tool_diary_write(
|
||||
agent_name="TestAgent",
|
||||
entry="A normal-length entry that fits comfortably under chunk_size.",
|
||||
topic="general",
|
||||
)
|
||||
assert r["success"] is True
|
||||
assert r["chunks"] == 1
|
||||
_client2, col = _get_collection(palace_path)
|
||||
del _client2
|
||||
assert col.count() == 1
|
||||
|
||||
def test_diary_write_oversized_entry_chunked(self, monkeypatch, config, palace_path, kg):
|
||||
"""Regression for #1539: an entry far above CHUNK_SIZE must be
|
||||
sliced into bounded per-chunk drawers, each linked by a
|
||||
``parent_entry_id`` metadata field. No single document stored
|
||||
may exceed CHUNK_SIZE."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_diary_write
|
||||
|
||||
# 5000 chars: well above CHUNK_SIZE=800. Expected chunks: ceil(5000/800) = 7.
|
||||
oversized = "Z" * 5000
|
||||
r = tool_diary_write(agent_name="TestAgent", entry=oversized, topic="general")
|
||||
|
||||
assert r["success"] is True
|
||||
assert r["chunks"] > 1, f"oversized entry must produce >1 chunks; got {r['chunks']}"
|
||||
assert "chunk_ids" in r and len(r["chunk_ids"]) == r["chunks"]
|
||||
|
||||
_client2, col = _get_collection(palace_path)
|
||||
del _client2
|
||||
stored = col.get()
|
||||
assert all(len(d) <= 800 for d in stored["documents"]), (
|
||||
f"no stored document may exceed CHUNK_SIZE=800; "
|
||||
f"got max={max(len(d) for d in stored['documents'])}"
|
||||
)
|
||||
joined = "".join(stored["documents"])
|
||||
assert joined == oversized, "joined chunks must equal original entry verbatim"
|
||||
|
||||
parent_ids = {m.get("parent_entry_id") for m in stored["metadatas"]}
|
||||
assert len(parent_ids) == 1 and None not in parent_ids, (
|
||||
f"all chunks must share one parent_entry_id; got {parent_ids}"
|
||||
)
|
||||
|
||||
def test_diary_write_chunk_index_metadata(self, monkeypatch, config, palace_path, kg):
|
||||
"""Regression for #1539: each oversized-entry chunk must carry a
|
||||
``chunk_index`` metadata field that runs 0, 1, 2, ... in order."""
|
||||
_patch_mcp_server(monkeypatch, config, kg)
|
||||
_client, _col = _get_collection(palace_path, create=True)
|
||||
del _client
|
||||
from mempalace.mcp_server import tool_diary_write
|
||||
|
||||
oversized = "Q" * 3500 # ~5 chunks at CHUNK_SIZE=800
|
||||
r = tool_diary_write(agent_name="TestAgent", entry=oversized, topic="general")
|
||||
assert r["success"] is True and r["chunks"] > 1
|
||||
|
||||
_client2, col = _get_collection(palace_path)
|
||||
del _client2
|
||||
stored = col.get()
|
||||
indices = sorted(m["chunk_index"] for m in stored["metadatas"])
|
||||
assert indices == list(range(len(indices))), (
|
||||
f"chunk_index must be 0..N-1 contiguous; got {indices}"
|
||||
)
|
||||
|
||||
|
||||
# ── Cache Invalidation (inode/mtime) ──────────────────────────────────
|
||||
|
||||
@@ -2121,9 +2377,9 @@ class TestCacheInvalidation:
|
||||
all_calls = captured["get"] + captured["create"]
|
||||
assert all_calls, "expected get_collection or create_collection to be called"
|
||||
for kwargs in all_calls:
|
||||
assert (
|
||||
"embedding_function" in kwargs
|
||||
), f"missing embedding_function= in chromadb call: {kwargs}"
|
||||
assert "embedding_function" in kwargs, (
|
||||
f"missing embedding_function= in chromadb call: {kwargs}"
|
||||
)
|
||||
assert kwargs["embedding_function"] is not None
|
||||
|
||||
# Same expectation on the create=False (cache-miss) reopen path.
|
||||
@@ -2295,6 +2551,115 @@ class TestKGLazyCache:
|
||||
query_a = mcp_server.tool_kg_query(entity="alice_secret")
|
||||
assert query_a.get("count", 0) >= 1, f"tenant A lost its own fact: {query_a}"
|
||||
|
||||
|
||||
# ── Structured error codes + MineAlreadyRunning (#1552) ─────────────────
|
||||
|
||||
|
||||
class TestStructuredErrors:
|
||||
"""Verify that _internal_tool_error and MineAlreadyRunning return
|
||||
machine-readable structured data (#1552)."""
|
||||
|
||||
def test_internal_tool_error_without_exc_has_no_data_field(self):
|
||||
"""Backward-compat: callers that omit exc still get a valid error dict."""
|
||||
from mempalace.mcp_server import _internal_tool_error
|
||||
|
||||
try:
|
||||
raise ValueError("test error")
|
||||
except ValueError:
|
||||
resp = _internal_tool_error("req-1", "mempalace_search")
|
||||
|
||||
assert resp["jsonrpc"] == "2.0"
|
||||
assert resp["id"] == "req-1"
|
||||
err = resp["error"]
|
||||
assert err["code"] == -32000
|
||||
assert err["message"] == "Internal tool error"
|
||||
assert "data" not in err
|
||||
|
||||
def test_internal_tool_error_with_exc_includes_structured_data(self):
|
||||
"""When exc is supplied, the error body must include data.error_class
|
||||
and data.message so callers can distinguish error types (#1552)."""
|
||||
from mempalace.mcp_server import _internal_tool_error
|
||||
|
||||
exc = RuntimeError("chromadb cold init wedge")
|
||||
try:
|
||||
raise exc
|
||||
except RuntimeError:
|
||||
resp = _internal_tool_error("req-2", "mempalace_add_drawer", exc)
|
||||
|
||||
err = resp["error"]
|
||||
assert err["code"] == -32000
|
||||
assert "data" in err
|
||||
assert err["data"]["error_class"] == "RuntimeError"
|
||||
assert "chromadb cold init wedge" in err["data"]["message"]
|
||||
|
||||
def test_internal_tool_error_exception_dispatch_passes_exc(self, monkeypatch):
|
||||
"""handle_request's Exception branch must pass exc to _internal_tool_error."""
|
||||
from mempalace import mcp_server
|
||||
|
||||
captured = {}
|
||||
|
||||
def fake_handler(**kwargs):
|
||||
raise OSError("fake disk error")
|
||||
|
||||
fake_tool_entry = {
|
||||
"handler": fake_handler,
|
||||
"input_schema": {"type": "object", "properties": {}},
|
||||
}
|
||||
monkeypatch.setattr(
|
||||
mcp_server,
|
||||
"TOOLS",
|
||||
{"mempalace_fake": fake_tool_entry},
|
||||
)
|
||||
|
||||
original = mcp_server._internal_tool_error
|
||||
|
||||
def spy_error(req_id, tool_name, exc=None):
|
||||
captured["exc"] = exc
|
||||
return original(req_id, tool_name, exc)
|
||||
|
||||
monkeypatch.setattr(mcp_server, "_internal_tool_error", spy_error)
|
||||
|
||||
req = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": "r1",
|
||||
"method": "tools/call",
|
||||
"params": {"name": "mempalace_fake", "arguments": {}},
|
||||
}
|
||||
resp = mcp_server.handle_request(req)
|
||||
assert resp["error"]["code"] == -32000
|
||||
assert isinstance(captured.get("exc"), OSError)
|
||||
assert "data" in resp["error"]
|
||||
assert resp["error"]["data"]["error_class"] == "OSError"
|
||||
|
||||
def test_tool_sync_mine_already_running_returns_error_class(self, monkeypatch, tmp_path):
|
||||
"""tool_sync MineAlreadyRunning path returns error_class: LockHeldByOtherProcess."""
|
||||
from mempalace import mcp_server
|
||||
from mempalace.palace import MineAlreadyRunning
|
||||
|
||||
cfg = MagicMock()
|
||||
cfg.palace_path = str(tmp_path / "palace")
|
||||
monkeypatch.setattr(mcp_server, "_config", cfg)
|
||||
monkeypatch.setattr(mcp_server, "_get_kg", lambda *a, **kw: MagicMock())
|
||||
|
||||
def _raise_locked(*args, **kwargs):
|
||||
raise MineAlreadyRunning("pid=12345")
|
||||
|
||||
import mempalace.sync as sync_mod
|
||||
|
||||
monkeypatch.setattr(sync_mod, "sync_palace", _raise_locked, raising=False)
|
||||
|
||||
result = mcp_server.tool_sync()
|
||||
assert result["success"] is False
|
||||
assert "another mine is in progress" in result["error"]
|
||||
assert result.get("error_class") == "LockHeldByOtherProcess"
|
||||
|
||||
def test_mcp_idle_timeout_invalid_env_disables_watchdog(self, monkeypatch):
|
||||
"""Invalid MEMPALACE_MCP_IDLE_HOURS disables idle auto-exit."""
|
||||
from mempalace import mcp_server
|
||||
|
||||
monkeypatch.setenv("MEMPALACE_MCP_IDLE_HOURS", "not-a-float")
|
||||
assert mcp_server._mcp_idle_timeout_secs() == 0.0
|
||||
|
||||
def test_cache_thread_safe(self, tmp_path, monkeypatch):
|
||||
"""Concurrent _get_kg() for the same path yields one instance."""
|
||||
import concurrent.futures
|
||||
@@ -2561,9 +2926,9 @@ class TestKGLazyCache:
|
||||
|
||||
result = mcp_server._canonicalize_kg_path("/some/Path/KG.sqlite3")
|
||||
|
||||
assert (
|
||||
result == "<NC:<RP:/some/Path/KG.sqlite3>>"
|
||||
), f"expected normcase(realpath(p)) composition, got {result!r}"
|
||||
assert result == "<NC:<RP:/some/Path/KG.sqlite3>>", (
|
||||
f"expected normcase(realpath(p)) composition, got {result!r}"
|
||||
)
|
||||
|
||||
def test_get_kg_dedupes_symlink_alias_end_to_end(self, tmp_path, monkeypatch):
|
||||
"""End-to-end: two ``_get_kg()`` calls via different symlink layers
|
||||
|
||||
@@ -78,6 +78,6 @@ def test_mcp_server_no_stdout_noise_on_clean_exit():
|
||||
capture_output=True,
|
||||
timeout=60,
|
||||
)
|
||||
assert (
|
||||
proc.stdout == b""
|
||||
), f"stdout must be empty before the first JSON-RPC response, but got: {proc.stdout!r}"
|
||||
assert proc.stdout == b"", (
|
||||
f"stdout must be empty before the first JSON-RPC response, but got: {proc.stdout!r}"
|
||||
)
|
||||
|
||||
+87
-1
@@ -1,10 +1,16 @@
|
||||
"""Tests for destructive-operation safety in mempalace.migrate."""
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from mempalace.migrate import collection_write_roundtrip_works, _restore_stale_palace, migrate
|
||||
from mempalace.migrate import (
|
||||
_restore_stale_palace,
|
||||
collection_write_roundtrip_works,
|
||||
extract_drawers_from_sqlite,
|
||||
migrate,
|
||||
)
|
||||
|
||||
|
||||
def test_migrate_requires_palace_database(tmp_path, capsys):
|
||||
@@ -157,6 +163,38 @@ def test_collection_write_roundtrip_fails_when_delete_silently_drops():
|
||||
assert len(col.ids) == 1
|
||||
|
||||
|
||||
def _make_minimal_chromadb_sqlite(tmp_path):
|
||||
"""Build a SQLite file with the minimal schema extract_drawers_from_sqlite reads."""
|
||||
db = tmp_path / "chroma.sqlite3"
|
||||
conn = sqlite3.connect(str(db))
|
||||
conn.executescript(
|
||||
"""
|
||||
CREATE TABLE embeddings (id INTEGER PRIMARY KEY, embedding_id TEXT);
|
||||
CREATE TABLE embedding_metadata (
|
||||
id INTEGER, key TEXT,
|
||||
string_value TEXT, int_value INTEGER,
|
||||
float_value REAL, bool_value INTEGER
|
||||
);
|
||||
INSERT INTO embeddings VALUES (1, 'd-001');
|
||||
INSERT INTO embedding_metadata VALUES (1, 'chroma:document', 'hello', NULL, NULL, NULL);
|
||||
INSERT INTO embedding_metadata VALUES (1, 'wing', 'personal', NULL, NULL, NULL);
|
||||
INSERT INTO embedding_metadata VALUES (1, 'room', '2026-04-26', NULL, NULL, NULL);
|
||||
"""
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return str(db)
|
||||
|
||||
|
||||
def test_extract_drawers_returns_drawers(tmp_path):
|
||||
db_path = _make_minimal_chromadb_sqlite(tmp_path)
|
||||
drawers = extract_drawers_from_sqlite(db_path)
|
||||
assert len(drawers) == 1
|
||||
assert drawers[0]["id"] == "d-001"
|
||||
assert drawers[0]["document"] == "hello"
|
||||
assert drawers[0]["metadata"] == {"wing": "personal", "room": "2026-04-26"}
|
||||
|
||||
|
||||
def test_migrate_dry_run_rebuilds_when_collection_is_readable_but_not_writable(tmp_path, capsys):
|
||||
palace_dir = tmp_path / "palace"
|
||||
palace_dir.mkdir()
|
||||
@@ -200,3 +238,51 @@ def test_migrate_dry_run_rebuilds_when_collection_is_readable_but_not_writable(t
|
||||
assert "Rebuilding from SQLite" in out
|
||||
assert "Extracted 1 drawers from SQLite" in out
|
||||
assert "DRY RUN" in out
|
||||
|
||||
|
||||
def test_migrate_cleans_temp_palace_on_chromadb_failure(tmp_path):
|
||||
"""If chromadb fails after the temp palace is created, mkdtemp's
|
||||
directory must be removed — without try/finally it leaked into the
|
||||
system temp root forever."""
|
||||
import tempfile as _tempfile
|
||||
|
||||
palace_dir = tmp_path / "palace"
|
||||
palace_dir.mkdir()
|
||||
(palace_dir / "chroma.sqlite3").write_text("db")
|
||||
|
||||
captured_temp_paths = []
|
||||
real_mkdtemp = _tempfile.mkdtemp
|
||||
|
||||
def tracking_mkdtemp(*args, **kwargs):
|
||||
path = real_mkdtemp(*args, **kwargs)
|
||||
captured_temp_paths.append(path)
|
||||
return path
|
||||
|
||||
failing_backend = MagicMock()
|
||||
# First ChromaBackend().get_collection() must raise so we drop into
|
||||
# the SQL-extraction path; the second ChromaBackend().get_or_create_collection()
|
||||
# raises to trigger the cleanup we are testing.
|
||||
failing_backend.get_collection.side_effect = Exception("unreadable")
|
||||
failing_backend.get_or_create_collection.side_effect = RuntimeError("chromadb boom")
|
||||
|
||||
import mempalace.backends.chroma as _chroma_mod
|
||||
|
||||
with (
|
||||
patch("mempalace.migrate.detect_chromadb_version", return_value="0.5.x"),
|
||||
patch(
|
||||
"mempalace.migrate.extract_drawers_from_sqlite",
|
||||
return_value=[{"id": "id1", "document": "doc", "metadata": {"wing": "w", "room": "r"}}],
|
||||
),
|
||||
patch("builtins.input", return_value="y"),
|
||||
patch("mempalace.migrate.shutil.copytree"),
|
||||
patch("mempalace.migrate.tempfile.mkdtemp", side_effect=tracking_mkdtemp),
|
||||
patch.object(_chroma_mod, "ChromaBackend", return_value=failing_backend),
|
||||
):
|
||||
try:
|
||||
migrate(str(palace_dir), confirm=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
assert captured_temp_paths, "mkdtemp was never called — flow short-circuited"
|
||||
for p in captured_temp_paths:
|
||||
assert not os.path.exists(p), f"temp palace was not cleaned up: {p}"
|
||||
|
||||
+513
-13
@@ -55,6 +55,199 @@ def test_project_mining():
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
|
||||
|
||||
def test_mine_computes_hallways_for_wing_post_mine(monkeypatch):
|
||||
"""After mine() completes (non-dry-run), compute_hallways_for_wing must be
|
||||
called once with the wing name and the live collection.
|
||||
|
||||
This is the integration test for the hallway primitive — without this
|
||||
call, the hallway module is dead code (no miner triggers it). Mirrors
|
||||
the existing tunnel-computation integration pattern at miner.py:1241.
|
||||
"""
|
||||
|
||||
from mempalace import miner as miner_mod
|
||||
|
||||
hallway_calls = []
|
||||
|
||||
def fake_compute(wing, col=None, min_count=2):
|
||||
hallway_calls.append({"wing": wing, "col": col, "min_count": min_count})
|
||||
return [] # no hallways materialized — that's not what we're testing
|
||||
|
||||
# Patch at the call site (mempalace.miner.compute_hallways_for_wing) so
|
||||
# the integration in mine() routes through our stub.
|
||||
monkeypatch.setattr(miner_mod, "compute_hallways_for_wing", fake_compute)
|
||||
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
project_root = Path(tmpdir).resolve()
|
||||
os.makedirs(project_root / "backend")
|
||||
write_file(
|
||||
project_root / "backend" / "app.py",
|
||||
"def main():\n print('hello world')\n" * 20,
|
||||
)
|
||||
with open(project_root / "mempalace.yaml", "w") as f:
|
||||
yaml.dump(
|
||||
{
|
||||
"wing": "test_project",
|
||||
"rooms": [{"name": "backend", "description": "Backend code"}],
|
||||
},
|
||||
f,
|
||||
)
|
||||
|
||||
palace_path = project_root / "palace"
|
||||
mine(str(project_root), str(palace_path))
|
||||
|
||||
# Must have been called exactly once, with our wing name + a live
|
||||
# collection. We don't pin min_count — the integration may pick a
|
||||
# default that differs from the function's own default.
|
||||
assert len(hallway_calls) == 1, (
|
||||
f"expected compute_hallways_for_wing to be called once, got {len(hallway_calls)}"
|
||||
)
|
||||
call = hallway_calls[0]
|
||||
assert call["wing"] == "test_project"
|
||||
assert call["col"] is not None, (
|
||||
"must pass the live collection so hallways can query drawers"
|
||||
)
|
||||
finally:
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
|
||||
|
||||
def test_mine_hallway_failure_does_not_crash_mine(monkeypatch):
|
||||
"""If compute_hallways_for_wing raises, the mine must still complete.
|
||||
|
||||
Mirrors the try/except wrap around the existing tunnel-computation block
|
||||
at miner.py:1244-1249. Hallway computation is a derived analytic, not
|
||||
load-bearing for the drawer write itself.
|
||||
"""
|
||||
from mempalace import miner as miner_mod
|
||||
|
||||
def angry_compute(wing, col=None, min_count=2):
|
||||
raise RuntimeError("simulated hallway-compute explosion")
|
||||
|
||||
monkeypatch.setattr(miner_mod, "compute_hallways_for_wing", angry_compute)
|
||||
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
project_root = Path(tmpdir).resolve()
|
||||
os.makedirs(project_root / "backend")
|
||||
write_file(
|
||||
project_root / "backend" / "app.py",
|
||||
"def main():\n print('hello world')\n" * 20,
|
||||
)
|
||||
with open(project_root / "mempalace.yaml", "w") as f:
|
||||
yaml.dump(
|
||||
{
|
||||
"wing": "test_project",
|
||||
"rooms": [{"name": "backend", "description": "Backend code"}],
|
||||
},
|
||||
f,
|
||||
)
|
||||
|
||||
palace_path = project_root / "palace"
|
||||
# Must NOT raise — the failure has to be caught + logged but not propagated.
|
||||
mine(str(project_root), str(palace_path))
|
||||
|
||||
# Drawer-write side of the mine still committed.
|
||||
client = chromadb.PersistentClient(path=str(palace_path))
|
||||
col = client.get_collection("mempalace_drawers")
|
||||
assert col.count() > 0
|
||||
finally:
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
|
||||
|
||||
def test_mine_computes_entity_tunnels_for_wing_post_mine(monkeypatch):
|
||||
"""After mine() completes (non-dry-run), ``_compute_entity_tunnels_for_wing``
|
||||
must be called once with the wing name from mempalace.yaml.
|
||||
|
||||
Without this call the entity-tunnel feature is dead code — no miner
|
||||
triggers it, no entity tunnels ever land in ~/.mempalace/tunnels.json.
|
||||
Mirrors the existing hallway- and topic-tunnel integration tests in
|
||||
this file.
|
||||
"""
|
||||
from mempalace import miner as miner_mod
|
||||
|
||||
entity_tunnel_calls = []
|
||||
|
||||
def fake_compute(wing):
|
||||
entity_tunnel_calls.append({"wing": wing})
|
||||
return 0 # no tunnels — that's not what we're testing here
|
||||
|
||||
# Patch at the call site (mempalace.miner._compute_entity_tunnels_for_wing)
|
||||
# so the integration in mine() routes through our stub.
|
||||
monkeypatch.setattr(miner_mod, "_compute_entity_tunnels_for_wing", fake_compute)
|
||||
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
project_root = Path(tmpdir).resolve()
|
||||
os.makedirs(project_root / "backend")
|
||||
write_file(
|
||||
project_root / "backend" / "app.py",
|
||||
"def main():\n print('hello world')\n" * 20,
|
||||
)
|
||||
with open(project_root / "mempalace.yaml", "w") as f:
|
||||
yaml.dump(
|
||||
{
|
||||
"wing": "test_project",
|
||||
"rooms": [{"name": "backend", "description": "Backend code"}],
|
||||
},
|
||||
f,
|
||||
)
|
||||
|
||||
palace_path = project_root / "palace"
|
||||
mine(str(project_root), str(palace_path))
|
||||
|
||||
assert len(entity_tunnel_calls) == 1, (
|
||||
f"expected _compute_entity_tunnels_for_wing to be called once, "
|
||||
f"got {len(entity_tunnel_calls)}"
|
||||
)
|
||||
assert entity_tunnel_calls[0]["wing"] == "test_project"
|
||||
finally:
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
|
||||
|
||||
def test_mine_entity_tunnel_failure_does_not_crash_mine(monkeypatch):
|
||||
"""If ``_compute_entity_tunnels_for_wing`` raises, the mine must still
|
||||
complete and the drawer write must remain committed.
|
||||
|
||||
Mirrors the try/except wrap around the existing tunnel- and hallway-
|
||||
computation blocks. Entity tunnel computation is a derived analytic,
|
||||
not load-bearing for the drawer write itself.
|
||||
"""
|
||||
from mempalace import miner as miner_mod
|
||||
|
||||
def angry_compute(wing):
|
||||
raise RuntimeError("simulated entity-tunnel-compute explosion")
|
||||
|
||||
monkeypatch.setattr(miner_mod, "_compute_entity_tunnels_for_wing", angry_compute)
|
||||
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
project_root = Path(tmpdir).resolve()
|
||||
os.makedirs(project_root / "backend")
|
||||
write_file(
|
||||
project_root / "backend" / "app.py",
|
||||
"def main():\n print('hello world')\n" * 20,
|
||||
)
|
||||
with open(project_root / "mempalace.yaml", "w") as f:
|
||||
yaml.dump(
|
||||
{
|
||||
"wing": "test_project",
|
||||
"rooms": [{"name": "backend", "description": "Backend code"}],
|
||||
},
|
||||
f,
|
||||
)
|
||||
|
||||
palace_path = project_root / "palace"
|
||||
# Must NOT raise — the failure has to be caught + logged but not propagated.
|
||||
mine(str(project_root), str(palace_path))
|
||||
|
||||
# Drawer-write side of the mine still committed.
|
||||
client = chromadb.PersistentClient(path=str(palace_path))
|
||||
col = client.get_collection("mempalace_drawers")
|
||||
assert col.count() > 0
|
||||
finally:
|
||||
shutil.rmtree(tmpdir, ignore_errors=True)
|
||||
|
||||
|
||||
def test_load_config_uses_defaults_when_yaml_missing():
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
@@ -340,6 +533,40 @@ def test_entity_metadata_finds_cyrillic_names(monkeypatch):
|
||||
assert "Михаил" in result, f"Cyrillic name not found in entity metadata: {result!r}"
|
||||
|
||||
|
||||
def test_entity_metadata_matches_known_names_case_insensitively(monkeypatch):
|
||||
"""Per-drawer entity tagging must mirror init-time case-insensitive matching.
|
||||
|
||||
The init-time scanner in entity_detector.py already does case-insensitive
|
||||
matching against the corpus (line 276: ``name_line_indices = [...if name_lower
|
||||
in line.lower()...]``). The per-drawer tagger in miner.py:788 was not
|
||||
updated to use the same flag — so an entry like ``"Aya"`` in
|
||||
known_entities.json fails to match the lowercase mention ``aya`` that
|
||||
appears in chat transcripts, voice-typed notes, and journal-style
|
||||
drawers. This test pins down the contract: known-entity names must match
|
||||
the content case-insensitively.
|
||||
"""
|
||||
from mempalace import miner
|
||||
|
||||
# Stub the known-entity registry to a controlled set
|
||||
monkeypatch.setattr(miner, "_load_known_entities", lambda: frozenset({"Aya", "Lumi"}))
|
||||
|
||||
# Lowercase mentions of seeded names must still be tagged.
|
||||
result = miner._extract_entities_for_metadata("aya talked to lumi today about the palace.")
|
||||
matched = set(result.split(";")) if result else set()
|
||||
assert "Aya" in matched, (
|
||||
f"lowercase 'aya' must match the seeded 'Aya' (case-insensitive). Got: {matched!r}"
|
||||
)
|
||||
assert "Lumi" in matched, (
|
||||
f"lowercase 'lumi' must match the seeded 'Lumi' (case-insensitive). Got: {matched!r}"
|
||||
)
|
||||
|
||||
# Mixed case must also match
|
||||
result_mixed = miner._extract_entities_for_metadata("Aya saw lumi. AYA waved.")
|
||||
matched_mixed = set(result_mixed.split(";")) if result_mixed else set()
|
||||
assert "Aya" in matched_mixed
|
||||
assert "Lumi" in matched_mixed
|
||||
|
||||
|
||||
def test_file_already_mined_check_mtime():
|
||||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
@@ -608,7 +835,7 @@ def test_process_file_uses_bounded_upsert_batches(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(miner, "detect_hall", lambda content: "code")
|
||||
monkeypatch.setattr(miner, "_extract_entities_for_metadata", lambda content: "")
|
||||
|
||||
drawers, room = miner.process_file(
|
||||
drawers, room, skip_reason = miner.process_file(
|
||||
source,
|
||||
tmp_path,
|
||||
col,
|
||||
@@ -620,6 +847,7 @@ def test_process_file_uses_bounded_upsert_batches(tmp_path, monkeypatch):
|
||||
|
||||
assert drawers == 5
|
||||
assert room == "general"
|
||||
assert skip_reason is None
|
||||
assert col.batch_sizes == [2, 2, 1]
|
||||
|
||||
|
||||
@@ -809,7 +1037,8 @@ def test_mine_creates_topic_tunnels_for_shared_topics(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(miner, "_ENTITY_REGISTRY_PATH", str(registry))
|
||||
miner._ENTITY_REGISTRY_CACHE.update({"mtime": None, "names": frozenset(), "raw": {}})
|
||||
tunnels_file = tmp_path / "tunnels.json"
|
||||
monkeypatch.setattr(palace_graph, "_TUNNEL_FILE", str(tunnels_file))
|
||||
monkeypatch.setattr(palace_graph, "_get_tunnel_file", lambda *a, **kw: str(tunnels_file))
|
||||
monkeypatch.setattr(palace_graph, "_legacy_tunnel_file", lambda: str(tunnels_file) + ".legacy")
|
||||
|
||||
# Pre-populate the registry as if init had been run for two wings that
|
||||
# share a topic.
|
||||
@@ -850,7 +1079,8 @@ def test_mine_no_tunnel_when_threshold_blocks_overlap(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(miner, "_ENTITY_REGISTRY_PATH", str(registry))
|
||||
miner._ENTITY_REGISTRY_CACHE.update({"mtime": None, "names": frozenset(), "raw": {}})
|
||||
tunnels_file = tmp_path / "tunnels.json"
|
||||
monkeypatch.setattr(palace_graph, "_TUNNEL_FILE", str(tunnels_file))
|
||||
monkeypatch.setattr(palace_graph, "_get_tunnel_file", lambda *a, **kw: str(tunnels_file))
|
||||
monkeypatch.setattr(palace_graph, "_legacy_tunnel_file", lambda: str(tunnels_file) + ".legacy")
|
||||
monkeypatch.setenv("MEMPALACE_TOPIC_TUNNEL_MIN_COUNT", "2")
|
||||
|
||||
miner.add_to_known_entities({"topics": ["foo"]}, wing="wing_one")
|
||||
@@ -880,7 +1110,8 @@ def test_mine_no_tunnel_when_only_one_wing_has_topics(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(miner, "_ENTITY_REGISTRY_PATH", str(registry))
|
||||
miner._ENTITY_REGISTRY_CACHE.update({"mtime": None, "names": frozenset(), "raw": {}})
|
||||
tunnels_file = tmp_path / "tunnels.json"
|
||||
monkeypatch.setattr(palace_graph, "_TUNNEL_FILE", str(tunnels_file))
|
||||
monkeypatch.setattr(palace_graph, "_get_tunnel_file", lambda *a, **kw: str(tunnels_file))
|
||||
monkeypatch.setattr(palace_graph, "_legacy_tunnel_file", lambda: str(tunnels_file) + ".legacy")
|
||||
|
||||
miner.add_to_known_entities({"topics": ["foo"]}, wing="wing_one")
|
||||
|
||||
@@ -935,7 +1166,7 @@ def test_mine_keyboard_interrupt_prints_summary_and_exits_130(tmp_path, capsys):
|
||||
call_count["n"] += 1
|
||||
if call_count["n"] == 2:
|
||||
raise KeyboardInterrupt
|
||||
return (1, "general")
|
||||
return (1, "general", None)
|
||||
|
||||
with patch("mempalace.miner.process_file", side_effect=fake_process_file):
|
||||
with pytest.raises(SystemExit) as exc_info:
|
||||
@@ -988,16 +1219,18 @@ def test_skip_filenames_includes_lockfiles():
|
||||
assert "yarn.lock" in miner.SKIP_FILENAMES
|
||||
|
||||
|
||||
def test_process_file_skips_when_chunks_exceed_max(tmp_path, monkeypatch):
|
||||
"""A file producing more than MAX_CHUNKS_PER_FILE chunks must be skipped
|
||||
with a clear message and zero upserts. Generated artifacts (CSVs, lock
|
||||
files not in SKIP_FILENAMES) hit this — the cap is what prevents ONNX
|
||||
bad_alloc on Windows when the embedder is asked to swallow thousands of
|
||||
chunks in one batch (#1296)."""
|
||||
def test_process_file_skips_when_chunks_exceed_max(tmp_path, monkeypatch, capsys):
|
||||
"""A file exceeding the per-file chunk cap is skipped with a tagged
|
||||
return and a stderr/stdout message pointing at the config override. The
|
||||
cap is the rail against pathological artifacts (CSVs, lockfiles not in
|
||||
SKIP_FILENAMES) and against ONNX bad_alloc on Windows (#1296); #1455
|
||||
raised the default and made the cap configurable so legitimate
|
||||
long-form content is not silently dropped."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.delenv("MEMPALACE_MAX_CHUNKS_PER_FILE", raising=False)
|
||||
monkeypatch.setattr(miner, "MAX_CHUNKS_PER_FILE", 5)
|
||||
over_cap = [{"content": f"chunk {i}", "chunk_index": i} for i in range(7)]
|
||||
monkeypatch.setattr(miner, "chunk_text", lambda content, source_file, **kwargs: over_cap)
|
||||
@@ -1007,7 +1240,7 @@ def test_process_file_skips_when_chunks_exceed_max(tmp_path, monkeypatch):
|
||||
col = MagicMock()
|
||||
col.get.return_value = {"ids": []}
|
||||
|
||||
drawers, room = miner.process_file(
|
||||
drawers, room, skip_reason = miner.process_file(
|
||||
source,
|
||||
tmp_path,
|
||||
col,
|
||||
@@ -1018,7 +1251,274 @@ def test_process_file_skips_when_chunks_exceed_max(tmp_path, monkeypatch):
|
||||
)
|
||||
|
||||
assert drawers == 0
|
||||
assert skip_reason == "chunk_cap"
|
||||
col.upsert.assert_not_called()
|
||||
captured = capsys.readouterr()
|
||||
# Skip notice goes to stderr to match the existing symlink-skip
|
||||
# convention in ``scan_project`` so log piping stays coherent.
|
||||
assert "[skip]" in captured.err
|
||||
assert "--max-chunks-per-file" in captured.err
|
||||
assert "MEMPALACE_MAX_CHUNKS_PER_FILE" in captured.err
|
||||
assert "[skip]" not in captured.out
|
||||
|
||||
|
||||
def test_resolve_max_chunks_default_when_no_override_no_env(monkeypatch):
|
||||
"""With no override and no env var, the module-level default applies."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.delenv("MEMPALACE_MAX_CHUNKS_PER_FILE", raising=False)
|
||||
assert miner._resolve_max_chunks_per_file(None) == miner.MAX_CHUNKS_PER_FILE
|
||||
|
||||
|
||||
def test_resolve_max_chunks_env_var_wins_over_default(monkeypatch):
|
||||
"""A numeric env var overrides the module default."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.setenv("MEMPALACE_MAX_CHUNKS_PER_FILE", "777")
|
||||
assert miner._resolve_max_chunks_per_file(None) == 777
|
||||
|
||||
|
||||
def test_resolve_max_chunks_override_wins_over_env(monkeypatch):
|
||||
"""An explicit override (CLI flag plumbed in) wins over the env var."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.setenv("MEMPALACE_MAX_CHUNKS_PER_FILE", "777")
|
||||
assert miner._resolve_max_chunks_per_file(123) == 123
|
||||
|
||||
|
||||
def test_resolve_max_chunks_sentinel_zero_disables(monkeypatch):
|
||||
"""Sentinel ``0`` from override or env means "no cap"."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.delenv("MEMPALACE_MAX_CHUNKS_PER_FILE", raising=False)
|
||||
assert miner._resolve_max_chunks_per_file(0) == 0
|
||||
|
||||
monkeypatch.setenv("MEMPALACE_MAX_CHUNKS_PER_FILE", "0")
|
||||
assert miner._resolve_max_chunks_per_file(None) == 0
|
||||
|
||||
|
||||
def test_resolve_max_chunks_invalid_env_falls_back_to_default(monkeypatch, capsys):
|
||||
"""A non-integer env value warns and uses the module default. This
|
||||
keeps a misconfigured shell from silently dropping content."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.setenv("MEMPALACE_MAX_CHUNKS_PER_FILE", "banana")
|
||||
assert miner._resolve_max_chunks_per_file(None) == miner.MAX_CHUNKS_PER_FILE
|
||||
err = capsys.readouterr().err
|
||||
assert "MEMPALACE_MAX_CHUNKS_PER_FILE" in err
|
||||
assert "banana" in err
|
||||
|
||||
|
||||
def test_resolve_max_chunks_negative_env_falls_back_to_default(monkeypatch, capsys):
|
||||
"""A negative env value warns and uses the module default."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.setenv("MEMPALACE_MAX_CHUNKS_PER_FILE", "-5")
|
||||
assert miner._resolve_max_chunks_per_file(None) == miner.MAX_CHUNKS_PER_FILE
|
||||
err = capsys.readouterr().err
|
||||
assert "MEMPALACE_MAX_CHUNKS_PER_FILE" in err
|
||||
assert "-5" in err
|
||||
|
||||
|
||||
def test_process_file_sentinel_zero_disables_cap(tmp_path, monkeypatch):
|
||||
"""With ``max_chunks_per_file=0`` even a pathologically large chunk
|
||||
count is processed (no skip)."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.delenv("MEMPALACE_MAX_CHUNKS_PER_FILE", raising=False)
|
||||
monkeypatch.setattr(miner, "MAX_CHUNKS_PER_FILE", 5)
|
||||
big = [{"content": f"chunk {i}", "chunk_index": i} for i in range(20)]
|
||||
monkeypatch.setattr(miner, "chunk_text", lambda content, source_file, **kwargs: big)
|
||||
monkeypatch.setattr(miner, "detect_room", lambda *a, **k: "general")
|
||||
monkeypatch.setattr(miner, "_extract_entities_for_metadata", lambda content: "")
|
||||
monkeypatch.setattr(miner, "build_closet_lines", lambda *a, **k: [])
|
||||
monkeypatch.setattr(miner, "purge_file_closets", lambda *a, **k: None)
|
||||
monkeypatch.setattr(miner, "upsert_closet_lines", lambda *a, **k: None)
|
||||
|
||||
source = tmp_path / "huge.csv"
|
||||
source.write_text("payload\n" * 500, encoding="utf-8")
|
||||
col = MagicMock()
|
||||
col.get.return_value = {"ids": []}
|
||||
|
||||
drawers, _room, skip_reason = miner.process_file(
|
||||
source,
|
||||
tmp_path,
|
||||
col,
|
||||
"wing",
|
||||
[{"name": "general", "description": "General"}],
|
||||
"agent",
|
||||
False,
|
||||
max_chunks_per_file=0,
|
||||
)
|
||||
|
||||
assert drawers == 20
|
||||
assert skip_reason is None
|
||||
col.upsert.assert_called()
|
||||
|
||||
|
||||
def test_mine_summary_separates_chunk_cap_skips(tmp_path, monkeypatch, capsys):
|
||||
"""Summary distinguishes residual skips from "chunk cap" skips so a
|
||||
user can see immediately that legitimate content was dropped (#1455).
|
||||
The chunk-cap summary line appears only when count > 0."""
|
||||
from unittest.mock import patch
|
||||
|
||||
project_root = tmp_path / "proj"
|
||||
project_root.mkdir()
|
||||
_make_minable_project(project_root, n_files=3)
|
||||
palace_path = project_root / "palace"
|
||||
|
||||
seq = iter(
|
||||
[
|
||||
(5, "general", None),
|
||||
(0, "general", "chunk_cap"),
|
||||
(0, "general", None),
|
||||
]
|
||||
)
|
||||
|
||||
def fake_process_file(*args, **kwargs):
|
||||
return next(seq)
|
||||
|
||||
with patch("mempalace.miner.process_file", side_effect=fake_process_file):
|
||||
mine(str(project_root), str(palace_path))
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "Files skipped (already filed or other): 1" in out
|
||||
assert "Files skipped (chunk cap" in out
|
||||
assert "--max-chunks-per-file" in out
|
||||
|
||||
|
||||
def test_mine_summary_omits_chunk_cap_line_when_zero(tmp_path, monkeypatch, capsys):
|
||||
"""When no file hits the chunk cap, the chunk-cap summary line is not
|
||||
printed at all, which keeps the happy-path output unchanged."""
|
||||
from unittest.mock import patch
|
||||
|
||||
project_root = tmp_path / "proj"
|
||||
project_root.mkdir()
|
||||
_make_minable_project(project_root, n_files=2)
|
||||
palace_path = project_root / "palace"
|
||||
|
||||
def fake_process_file(*args, **kwargs):
|
||||
return (3, "general", None)
|
||||
|
||||
with patch("mempalace.miner.process_file", side_effect=fake_process_file):
|
||||
mine(str(project_root), str(palace_path))
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "Files skipped (already filed or other): 0" in out
|
||||
assert "chunk cap" not in out
|
||||
|
||||
|
||||
def test_resolve_max_chunks_negative_override_falls_back_to_default(monkeypatch, capsys):
|
||||
"""A negative CLI override warns and falls back to the module default.
|
||||
Symmetric with the env-var path so ``--max-chunks-per-file=-500`` (a
|
||||
typo meaning "no, don't lower it that much") does not silently
|
||||
disable the cap and OOM the embedder on the original lockfile."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.delenv("MEMPALACE_MAX_CHUNKS_PER_FILE", raising=False)
|
||||
assert miner._resolve_max_chunks_per_file(-5) == miner.MAX_CHUNKS_PER_FILE
|
||||
err = capsys.readouterr().err
|
||||
assert "--max-chunks-per-file" in err
|
||||
assert "-5" in err
|
||||
|
||||
|
||||
def test_resolve_max_chunks_reads_module_attribute_at_call_time(monkeypatch):
|
||||
"""The resolver reads ``miner.MAX_CHUNKS_PER_FILE`` lazily, so a
|
||||
monkeypatch landed at test setup is honored. Regression guard against
|
||||
a future refactor that captures the import-time default."""
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.delenv("MEMPALACE_MAX_CHUNKS_PER_FILE", raising=False)
|
||||
monkeypatch.setattr(miner, "MAX_CHUNKS_PER_FILE", 7)
|
||||
assert miner._resolve_max_chunks_per_file(None) == 7
|
||||
|
||||
|
||||
def test_process_file_chunk_cap_under_dry_run(tmp_path, monkeypatch):
|
||||
"""Dry-run is the natural audit path for #1455; chunk-cap drops must
|
||||
return a tagged skip_reason there too so the summary counter can fire."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from mempalace import miner
|
||||
|
||||
monkeypatch.delenv("MEMPALACE_MAX_CHUNKS_PER_FILE", raising=False)
|
||||
monkeypatch.setattr(miner, "MAX_CHUNKS_PER_FILE", 5)
|
||||
over_cap = [{"content": f"chunk {i}", "chunk_index": i} for i in range(7)]
|
||||
monkeypatch.setattr(miner, "chunk_text", lambda content, source_file, **kwargs: over_cap)
|
||||
|
||||
source = tmp_path / "big.csv"
|
||||
source.write_text("payload\n" * 200, encoding="utf-8")
|
||||
col = MagicMock()
|
||||
col.get.return_value = {"ids": []}
|
||||
|
||||
drawers, _room, skip_reason = miner.process_file(
|
||||
source,
|
||||
tmp_path,
|
||||
col,
|
||||
"wing",
|
||||
[{"name": "general", "description": "General"}],
|
||||
"agent",
|
||||
True, # dry_run
|
||||
)
|
||||
|
||||
assert drawers == 0
|
||||
assert skip_reason == "chunk_cap"
|
||||
|
||||
|
||||
def test_mine_dry_run_summary_counts_chunk_cap_drops(tmp_path, capsys):
|
||||
"""The summary under dry-run also splits out chunk-cap skips. Without
|
||||
this a reporter running ``--dry-run`` to validate the new default
|
||||
against their corpus would see "Files processed: N / Files skipped: 0"
|
||||
even when chunk-cap drops occurred, which is exactly the silent-drop
|
||||
UX bug that #1455 is fixing."""
|
||||
from unittest.mock import patch
|
||||
|
||||
project_root = tmp_path / "proj"
|
||||
project_root.mkdir()
|
||||
_make_minable_project(project_root, n_files=3)
|
||||
palace_path = project_root / "palace"
|
||||
|
||||
seq = iter(
|
||||
[
|
||||
(5, "general", None),
|
||||
(0, "general", "chunk_cap"),
|
||||
(4, "general", None),
|
||||
]
|
||||
)
|
||||
|
||||
def fake_process_file(*args, **kwargs):
|
||||
return next(seq)
|
||||
|
||||
with patch("mempalace.miner.process_file", side_effect=fake_process_file):
|
||||
mine(str(project_root), str(palace_path), dry_run=True)
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "Files skipped (chunk cap" in out
|
||||
assert "1 (raise via" in out
|
||||
|
||||
|
||||
def test_mine_plumbs_max_chunks_per_file_to_process_file(tmp_path):
|
||||
"""``mine(max_chunks_per_file=0)`` reaches ``process_file`` as kwarg=0,
|
||||
enabling the sentinel-disable path end-to-end. Guards the wiring
|
||||
``cmd_mine -> mine -> _mine_impl -> process_file``."""
|
||||
from unittest.mock import patch
|
||||
|
||||
project_root = tmp_path / "proj"
|
||||
project_root.mkdir()
|
||||
_make_minable_project(project_root, n_files=1)
|
||||
palace_path = project_root / "palace"
|
||||
|
||||
captured = {}
|
||||
|
||||
def fake_process_file(*args, **kwargs):
|
||||
captured["max_chunks_per_file"] = kwargs.get("max_chunks_per_file")
|
||||
return (1, "general", None)
|
||||
|
||||
with patch("mempalace.miner.process_file", side_effect=fake_process_file):
|
||||
mine(str(project_root), str(palace_path), max_chunks_per_file=0)
|
||||
|
||||
assert captured["max_chunks_per_file"] == 0
|
||||
|
||||
|
||||
def test_mine_arbitrary_exception_prints_summary_and_reraises(tmp_path, capsys):
|
||||
@@ -1040,7 +1540,7 @@ def test_mine_arbitrary_exception_prints_summary_and_reraises(tmp_path, capsys):
|
||||
call_count["n"] += 1
|
||||
if call_count["n"] == 2:
|
||||
raise RuntimeError("simulated ONNX bad_alloc")
|
||||
return (1, "general")
|
||||
return (1, "general", None)
|
||||
|
||||
with patch("mempalace.miner.process_file", side_effect=fake_process_file):
|
||||
with pytest.raises(RuntimeError, match="simulated ONNX bad_alloc"):
|
||||
|
||||
@@ -1336,11 +1336,7 @@ class TestStripNoiseRemovesSystemChrome:
|
||||
|
||||
def test_strips_line_anchored_system_reminder_block(self):
|
||||
text = (
|
||||
"> User:\n"
|
||||
"<system-reminder>\n"
|
||||
"Auto-save reminder...\n"
|
||||
"</system-reminder>\n"
|
||||
"> Real message."
|
||||
"> User:\n<system-reminder>\nAuto-save reminder...\n</system-reminder>\n> Real message."
|
||||
)
|
||||
out = strip_noise(text)
|
||||
assert "system-reminder" not in out
|
||||
@@ -1350,7 +1346,7 @@ class TestStripNoiseRemovesSystemChrome:
|
||||
def test_strips_system_reminder_with_blockquote_prefix(self):
|
||||
# _messages_to_transcript prefixes lines with "> ", so the line
|
||||
# anchor must also accept that shape.
|
||||
text = "> User:\n" "> <system-reminder>Injected noise</system-reminder>\n" "> Real message."
|
||||
text = "> User:\n> <system-reminder>Injected noise</system-reminder>\n> Real message."
|
||||
out = strip_noise(text)
|
||||
assert "Injected noise" not in out
|
||||
assert "Real message." in out
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Tests for explicit tunnel helpers in mempalace.palace_graph."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import stat
|
||||
import sys
|
||||
@@ -12,8 +13,23 @@ with patch.dict("sys.modules", {"chromadb": MagicMock()}):
|
||||
|
||||
|
||||
def _use_tmp_tunnel_file(monkeypatch, tmp_path):
|
||||
"""Redirect both the tunnel-file resolver and the legacy-file check at the
|
||||
tmp_path so existing tests stay in the configured-path branch and don't
|
||||
accidentally trip the new legacy-file warning branch in _load_tunnels.
|
||||
|
||||
Also neutralizes ``_get_collection`` so the endpoint-existence validation
|
||||
added for #1468 falls through to the "can't verify, allow" branch by
|
||||
default. Tests that exercise the validation path supply their own stub
|
||||
via a subsequent monkeypatch.setattr.
|
||||
"""
|
||||
tunnel_file = tmp_path / "tunnels.json"
|
||||
monkeypatch.setattr(palace_graph, "_TUNNEL_FILE", str(tunnel_file))
|
||||
monkeypatch.setattr(palace_graph, "_get_tunnel_file", lambda *a, **kw: str(tunnel_file))
|
||||
monkeypatch.setattr(
|
||||
palace_graph,
|
||||
"_legacy_tunnel_file",
|
||||
lambda: str(tmp_path / "legacy-tunnels.json"),
|
||||
)
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda *a, **kw: None)
|
||||
return tunnel_file
|
||||
|
||||
|
||||
@@ -63,9 +79,9 @@ class TestTunnelStorage:
|
||||
assert file_mode == 0o600, f"tunnels.json mode is {oct(file_mode)}, expected 0o600"
|
||||
|
||||
parent_mode = stat.S_IMODE(os.stat(tunnel_file.parent).st_mode)
|
||||
assert (
|
||||
parent_mode == 0o700
|
||||
), f"tunnels.json parent dir mode is {oct(parent_mode)}, expected 0o700"
|
||||
assert parent_mode == 0o700, (
|
||||
f"tunnels.json parent dir mode is {oct(parent_mode)}, expected 0o700"
|
||||
)
|
||||
|
||||
|
||||
class TestExplicitTunnels:
|
||||
@@ -481,3 +497,353 @@ class TestHyphenatedWingNormalization:
|
||||
|
||||
assert len(palace_graph.follow_tunnels("mempalace_public", "auth")) == 1
|
||||
assert len(palace_graph.follow_tunnels("mempalace-public", "auth")) == 1
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Regression: tunnel file follows palace_path config (#1467)
|
||||
# =============================================================================
|
||||
class TestTunnelFileFollowsConfig:
|
||||
"""Bug A: prior to 3.3.6 the tunnel file was hardcoded at
|
||||
``~/.mempalace/tunnels.json`` regardless of MempalaceConfig.palace_path.
|
||||
Under any profile-isolated $HOME (subagent profiles, sandboxes, multi-tenant
|
||||
hosts) tunnels would write to a different file than drawers, so
|
||||
``create_tunnel`` would appear to succeed while the tunnel was invisible
|
||||
to every other process touching the configured palace.
|
||||
"""
|
||||
|
||||
def test_default_tunnel_file_unchanged(self):
|
||||
"""Regression: with default config, tunnel_file resolves to
|
||||
``~/.mempalace/tunnels.json`` so existing single-user installs are
|
||||
not silently relocated."""
|
||||
from mempalace.config import DEFAULT_PALACE_PATH, MempalaceConfig
|
||||
|
||||
cfg = MempalaceConfig()
|
||||
# Default palace_path is ~/.mempalace/palace, so tunnel is sibling.
|
||||
expected = os.path.join(os.path.dirname(DEFAULT_PALACE_PATH), "tunnels.json")
|
||||
assert cfg.tunnel_file == expected
|
||||
assert palace_graph._get_tunnel_file(cfg) == expected
|
||||
|
||||
def test_tunnel_file_follows_palace_path(self, tmp_path):
|
||||
"""Custom palace_path → tunnel sits beside the palace, not at the
|
||||
hardcoded legacy location."""
|
||||
from mempalace.config import MempalaceConfig
|
||||
|
||||
custom_dir = tmp_path / "custom-palace"
|
||||
cfg = MempalaceConfig(config_dir=tmp_path)
|
||||
cfg._file_config["palace_path"] = str(custom_dir)
|
||||
assert cfg.tunnel_file == str(tmp_path / "tunnels.json")
|
||||
assert palace_graph._get_tunnel_file(cfg) == str(tmp_path / "tunnels.json")
|
||||
|
||||
def test_load_tunnels_warns_on_orphaned_legacy_file(self, tmp_path, monkeypatch, caplog):
|
||||
"""When the configured tunnel file is missing but a legacy file
|
||||
exists at a different path, _load_tunnels logs a one-line warning
|
||||
naming both paths and returns []. Critically, it does NOT
|
||||
auto-migrate — silent merging risks clobbering newer data."""
|
||||
configured = tmp_path / "configured" / "tunnels.json"
|
||||
legacy = tmp_path / "legacy" / "tunnels.json"
|
||||
legacy.parent.mkdir(parents=True)
|
||||
legacy.write_text(
|
||||
'[{"id":"orphan","source":{"wing":"a","room":"r"},'
|
||||
'"target":{"wing":"b","room":"r"},"label":""}]',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(palace_graph, "_get_tunnel_file", lambda *a, **kw: str(configured))
|
||||
monkeypatch.setattr(palace_graph, "_legacy_tunnel_file", lambda: str(legacy))
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="mempalace_graph"):
|
||||
result = palace_graph._load_tunnels()
|
||||
|
||||
assert result == [], "must not auto-migrate from legacy file"
|
||||
assert str(legacy) in caplog.text
|
||||
assert str(configured) in caplog.text
|
||||
|
||||
def test_no_legacy_warning_when_paths_match(self, tmp_path, monkeypatch, caplog):
|
||||
"""If configured and legacy resolve to the same path (default install),
|
||||
we must not emit a misleading 'legacy file ignored' warning when the
|
||||
file simply doesn't exist yet."""
|
||||
same = tmp_path / "tunnels.json"
|
||||
monkeypatch.setattr(palace_graph, "_get_tunnel_file", lambda *a, **kw: str(same))
|
||||
monkeypatch.setattr(palace_graph, "_legacy_tunnel_file", lambda: str(same))
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="mempalace_graph"):
|
||||
assert palace_graph._load_tunnels() == []
|
||||
|
||||
assert "Legacy tunnels file" not in caplog.text
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Regression: create_tunnel validates explicit-tunnel endpoints (#1468)
|
||||
# =============================================================================
|
||||
class _StubCollection:
|
||||
"""Minimal chroma-collection stub for endpoint-validation tests."""
|
||||
|
||||
def __init__(self, existing_rooms):
|
||||
# existing_rooms is a set of (wing, room) tuples
|
||||
self.existing_rooms = set(existing_rooms)
|
||||
self.calls = []
|
||||
|
||||
def get(self, where=None, limit=None, include=None):
|
||||
self.calls.append(where)
|
||||
# Parse the {"$and": [{"wing": W}, {"room": R}]} where clause we issue.
|
||||
wing = room = None
|
||||
for clause in (where or {}).get("$and", []):
|
||||
if "wing" in clause:
|
||||
wing = clause["wing"]
|
||||
if "room" in clause:
|
||||
room = clause["room"]
|
||||
if (wing, room) in self.existing_rooms:
|
||||
return {"ids": ["drawer-1"]}
|
||||
return {"ids": []}
|
||||
|
||||
|
||||
class TestCreateTunnelEndpointValidation:
|
||||
"""Bug B: pre-3.3.6 ``create_tunnel`` only validated wing/room names
|
||||
were non-empty strings, never that the rooms actually existed in the
|
||||
chroma index. Combined with Bug A's read-bubble, callers could
|
||||
successfully create tunnels pointing at phantom endpoints and a
|
||||
follow-up ``list_tunnels`` would self-confirm via the same isolated
|
||||
file. The fix queries chroma for at least one drawer in each endpoint
|
||||
before persisting an explicit tunnel."""
|
||||
|
||||
def test_create_tunnel_rejects_nonexistent_target_room(self, tmp_path, monkeypatch):
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
col = _StubCollection({("wing_code", "auth")}) # only source exists
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda config=None: col)
|
||||
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
palace_graph.create_tunnel("wing_code", "auth", "wing_people", "phantom")
|
||||
msg = str(exc_info.value)
|
||||
assert "phantom" in msg
|
||||
assert "wing_people" in msg
|
||||
# And nothing was persisted.
|
||||
assert palace_graph.list_tunnels() == []
|
||||
|
||||
def test_create_tunnel_rejects_nonexistent_source_room(self, tmp_path, monkeypatch):
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
col = _StubCollection({("wing_people", "users")}) # only target exists
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda config=None: col)
|
||||
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
palace_graph.create_tunnel("wing_code", "phantom", "wing_people", "users")
|
||||
msg = str(exc_info.value)
|
||||
assert "phantom" in msg
|
||||
assert "wing_code" in msg
|
||||
|
||||
def test_create_tunnel_succeeds_when_both_rooms_exist(self, tmp_path, monkeypatch):
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
col = _StubCollection({("wing_code", "auth"), ("wing_people", "users")})
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda config=None: col)
|
||||
|
||||
t = palace_graph.create_tunnel(
|
||||
"wing_code", "auth", "wing_people", "users", label="verified"
|
||||
)
|
||||
assert t["label"] == "verified"
|
||||
assert len(palace_graph.list_tunnels()) == 1
|
||||
|
||||
def test_create_tunnel_skips_validation_when_collection_unreachable(
|
||||
self, tmp_path, monkeypatch
|
||||
):
|
||||
"""When chroma is unreachable (palace not yet created, transient
|
||||
failure, tests without a real backend), validation is skipped
|
||||
rather than fail-closed — matches the tolerance pattern used
|
||||
throughout palace_graph._get_collection()."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda config=None: None)
|
||||
|
||||
t = palace_graph.create_tunnel("wing_code", "any", "wing_people", "any", label="cold-start")
|
||||
assert t["label"] == "cold-start"
|
||||
|
||||
def test_create_tunnel_tolerates_collection_query_exception(self, tmp_path, monkeypatch):
|
||||
"""Permission errors / temporary chroma faults during the
|
||||
validation query must not block legitimate writes."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
|
||||
class _AngryCollection:
|
||||
def get(self, **kw):
|
||||
raise RuntimeError("simulated chroma fault")
|
||||
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda config=None: _AngryCollection())
|
||||
|
||||
# Should not raise — fall back to "allow" rather than fail-closed.
|
||||
t = palace_graph.create_tunnel("wing_code", "x", "wing_people", "y", label="best-effort")
|
||||
assert t["label"] == "best-effort"
|
||||
|
||||
def test_compute_topic_tunnels_skips_endpoint_validation(self, tmp_path, monkeypatch):
|
||||
"""Topic tunnels use synthetic ``topic:<name>`` room identifiers
|
||||
that don't correspond to real chroma rooms. The endpoint-existence
|
||||
check must skip kind != 'explicit', otherwise auto-derived
|
||||
cross-wing graph edges would all be rejected."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
# Empty collection — would reject if validation ran.
|
||||
col = _StubCollection(set())
|
||||
monkeypatch.setattr(palace_graph, "_get_collection", lambda config=None: col)
|
||||
|
||||
# Two wings share a topic — should produce a topic-tunnel even
|
||||
# though neither "topic:auth" room exists in the stub collection.
|
||||
topics_by_wing = {
|
||||
"wing_code": ["auth", "logging"],
|
||||
"wing_people": ["auth", "schema"],
|
||||
}
|
||||
palace_graph.compute_topic_tunnels(topics_by_wing, min_count=1)
|
||||
tunnels = palace_graph.list_tunnels()
|
||||
assert tunnels, "topic tunnels must persist regardless of room validation"
|
||||
assert all(t.get("kind") == "topic" for t in tunnels)
|
||||
# And the validation query was never invoked for topic tunnels.
|
||||
assert col.calls == []
|
||||
|
||||
|
||||
class TestEntityTunnels:
|
||||
"""Cross-wing entity tunnels (the Wing → Drawer-entities → Hallway →
|
||||
Tunnel sequence from the v4 architecture vision).
|
||||
|
||||
When an entity has within-wing hallways in two or more wings, an entity
|
||||
tunnel bridges those wings, anchored on the entity. This is the
|
||||
architectural counterpart to ``compute_topic_tunnels`` — same storage,
|
||||
same dedup, but the substrate is hallway records (entity-grounded)
|
||||
rather than raw topic words.
|
||||
|
||||
Endpoints use the synthetic room id ``entity:<name>`` so they can't
|
||||
collide with literal folder-derived rooms of the same name (mirrors
|
||||
the ``topic:<name>`` convention).
|
||||
|
||||
Topic tunnels are NOT removed by this work — both systems run in
|
||||
parallel for one release cycle so existing palaces don't lose
|
||||
tunnels between mines. Deprecation is a separate follow-up PR.
|
||||
"""
|
||||
|
||||
def test_entity_tunnels_creates_cross_wing_tunnel_for_shared_entity(
|
||||
self, tmp_path, monkeypatch
|
||||
):
|
||||
"""Ben appears in a hallway in wing_aya AND in wing_mempalace →
|
||||
exactly one entity tunnel between those two wings, anchored on Ben."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
hallways = [
|
||||
{"wing": "wing_aya", "entity_a": "Ben", "entity_b": "Aya"},
|
||||
{"wing": "wing_mempalace", "entity_a": "Ben", "entity_b": "Igor"},
|
||||
]
|
||||
created = palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
assert len(created) == 1
|
||||
tunnel = created[0]
|
||||
assert {tunnel["source"]["wing"], tunnel["target"]["wing"]} == {
|
||||
"wing_aya",
|
||||
"wing_mempalace",
|
||||
}
|
||||
# Endpoint is the synthetic entity room — same casing as the
|
||||
# stored entity, prefixed with ``entity:`` to prevent collision
|
||||
# with a literal folder room of the same name.
|
||||
assert tunnel["source"]["room"] == "entity:Ben"
|
||||
assert tunnel["target"]["room"] == "entity:Ben"
|
||||
assert tunnel["kind"] == "entity"
|
||||
# Label carries the entity name without the prefix.
|
||||
assert "Ben" in tunnel["label"]
|
||||
assert "entity:Ben" not in tunnel["label"]
|
||||
|
||||
def test_entity_tunnels_skips_entities_in_only_one_wing(self, tmp_path, monkeypatch):
|
||||
"""Aya has hallways only in wing_aya → no tunnel for her."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
hallways = [
|
||||
{"wing": "wing_aya", "entity_a": "Aya", "entity_b": "Lumi"},
|
||||
{"wing": "wing_aya", "entity_a": "Aya", "entity_b": "Ever"},
|
||||
]
|
||||
created = palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
assert created == []
|
||||
|
||||
def test_entity_tunnels_counts_entity_in_either_pair_position(self, tmp_path, monkeypatch):
|
||||
"""Entity may appear as entity_a in one hallway and entity_b in
|
||||
another. Both positions count toward 'this entity is in this wing'."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
hallways = [
|
||||
# Ben is entity_b in wing_aya
|
||||
{"wing": "wing_aya", "entity_a": "Aya", "entity_b": "Ben"},
|
||||
# Ben is entity_a in wing_mempalace
|
||||
{"wing": "wing_mempalace", "entity_a": "Ben", "entity_b": "Igor"},
|
||||
]
|
||||
created = palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
assert len(created) == 1
|
||||
assert "Ben" in created[0]["label"]
|
||||
|
||||
def test_entity_tunnels_three_wings_pairwise_from_focus_wing(self, tmp_path, monkeypatch):
|
||||
"""When Ben has hallways in {wing_aya, wing_mp, wing_lt} and we
|
||||
compute for wing_aya, we create wing_aya↔wing_mp AND wing_aya↔wing_lt
|
||||
only — NOT wing_mp↔wing_lt. The cross-wing tunnel between the two
|
||||
other wings is the job of whichever of those two mines next.
|
||||
"""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
hallways = [
|
||||
{"wing": "wing_aya", "entity_a": "Ben", "entity_b": "Aya"},
|
||||
{"wing": "wing_mempalace", "entity_a": "Ben", "entity_b": "Igor"},
|
||||
{"wing": "wing_lantern", "entity_a": "Ben", "entity_b": "Lumi"},
|
||||
]
|
||||
created = palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
assert len(created) == 2
|
||||
target_wings = set()
|
||||
for t in created:
|
||||
other = (
|
||||
t["target"]["wing"] if t["source"]["wing"] == "wing_aya" else t["source"]["wing"]
|
||||
)
|
||||
target_wings.add(other)
|
||||
assert target_wings == {"wing_mempalace", "wing_lantern"}
|
||||
|
||||
def test_entity_tunnels_idempotent_on_rerun(self, tmp_path, monkeypatch):
|
||||
"""Re-running on the same hallway data must not duplicate tunnels.
|
||||
``create_tunnel`` dedup-on-canonical-id is the guarantee — this test
|
||||
pins the contract."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
hallways = [
|
||||
{"wing": "wing_aya", "entity_a": "Ben", "entity_b": "Aya"},
|
||||
{"wing": "wing_mempalace", "entity_a": "Ben", "entity_b": "Igor"},
|
||||
]
|
||||
palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
listed = palace_graph.list_tunnels()
|
||||
# Only one stored tunnel even after two passes.
|
||||
entity_tunnels = [t for t in listed if t.get("kind") == "entity"]
|
||||
assert len(entity_tunnels) == 1
|
||||
|
||||
def test_entity_tunnels_retrievable_via_list_tunnels(self, tmp_path, monkeypatch):
|
||||
"""Once written, entity tunnels appear in the standard
|
||||
``list_tunnels`` query — readers don't need a separate API."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
hallways = [
|
||||
{"wing": "wing_aya", "entity_a": "Ben", "entity_b": "Aya"},
|
||||
{"wing": "wing_mempalace", "entity_a": "Ben", "entity_b": "Igor"},
|
||||
]
|
||||
created = palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
listed = palace_graph.list_tunnels()
|
||||
assert {t["id"] for t in listed} == {t["id"] for t in created}
|
||||
|
||||
def test_entity_tunnels_empty_hallways_is_noop(self, tmp_path, monkeypatch):
|
||||
"""No hallways → no tunnels. No crash."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
assert palace_graph.entity_tunnels_for_wing("wing_aya", []) == []
|
||||
|
||||
def test_entity_tunnels_unknown_wing_is_noop(self, tmp_path, monkeypatch):
|
||||
"""Wing that doesn't appear in any hallway → no tunnels (the wing
|
||||
has no entities to bridge from)."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
hallways = [
|
||||
{"wing": "wing_other", "entity_a": "Ben", "entity_b": "Aya"},
|
||||
]
|
||||
assert palace_graph.entity_tunnels_for_wing("wing_nonexistent", hallways) == []
|
||||
|
||||
def test_entity_tunnel_room_does_not_collide_with_literal_room(self, tmp_path, monkeypatch):
|
||||
"""A literal folder room ``Ben`` (created via explicit tunnel) and the
|
||||
synthetic entity room ``entity:Ben`` must produce distinct tunnels —
|
||||
not get deduped together by id collision."""
|
||||
_use_tmp_tunnel_file(monkeypatch, tmp_path)
|
||||
# Pre-create an explicit tunnel with a literal "Ben" room.
|
||||
palace_graph.create_tunnel(
|
||||
"wing_aya", "Ben", "wing_mempalace", "Ben", label="literal", kind="explicit"
|
||||
)
|
||||
hallways = [
|
||||
{"wing": "wing_aya", "entity_a": "Ben", "entity_b": "Aya"},
|
||||
{"wing": "wing_mempalace", "entity_a": "Ben", "entity_b": "Igor"},
|
||||
]
|
||||
palace_graph.entity_tunnels_for_wing("wing_aya", hallways)
|
||||
listed = palace_graph.list_tunnels()
|
||||
# Two distinct tunnels: one literal "Ben" → "Ben", one
|
||||
# "entity:Ben" → "entity:Ben". Different canonical IDs.
|
||||
assert len(listed) == 2
|
||||
kinds = {t.get("kind") for t in listed}
|
||||
assert kinds == {"explicit", "entity"}
|
||||
|
||||
@@ -199,9 +199,9 @@ def test_reentrant_same_thread_passes_through(tmp_path, monkeypatch):
|
||||
child = ctx.Process(target=_try_acquire_expect_busy, args=(palace, result_q))
|
||||
try:
|
||||
child.start()
|
||||
assert (
|
||||
result_q.get(timeout=10) == "busy"
|
||||
), "outer lock should still be held by parent after inner re-entrant exit"
|
||||
assert result_q.get(timeout=10) == "busy", (
|
||||
"outer lock should still be held by parent after inner re-entrant exit"
|
||||
)
|
||||
child.join(timeout=5)
|
||||
assert child.exitcode == 0
|
||||
finally:
|
||||
@@ -267,9 +267,9 @@ def test_lock_failure_message_names_holder(tmp_path, monkeypatch):
|
||||
pytest.fail("second acquire of same palace should have raised")
|
||||
|
||||
msg = str(excinfo.value)
|
||||
assert (
|
||||
f"PID {holder_pid}" in msg
|
||||
), f"lock-failure message must name the holder PID; got: {msg!r}"
|
||||
assert f"PID {holder_pid}" in msg, (
|
||||
f"lock-failure message must name the holder PID; got: {msg!r}"
|
||||
)
|
||||
finally:
|
||||
open(release, "w").close()
|
||||
holder.join(timeout=5)
|
||||
|
||||
+15
-15
@@ -597,13 +597,13 @@ class TestBackendAbstraction:
|
||||
"""Claim: pluggable backends.
|
||||
backends/base.py must define an abstract base class."""
|
||||
path = MEMPALACE_PKG / "backends" / "base.py"
|
||||
assert (
|
||||
path.is_file()
|
||||
), "mempalace/backends/base.py does not exist. Backend abstraction layer is missing."
|
||||
assert path.is_file(), (
|
||||
"mempalace/backends/base.py does not exist. Backend abstraction layer is missing."
|
||||
)
|
||||
src = _read(path)
|
||||
assert (
|
||||
"ABC" in src or "abstractmethod" in src
|
||||
), "backends/base.py does not define an abstract base class."
|
||||
assert "ABC" in src or "abstractmethod" in src, (
|
||||
"backends/base.py does not define an abstract base class."
|
||||
)
|
||||
|
||||
def test_backends_chroma_exists(self):
|
||||
"""Claim: ChromaDB backend implementation.
|
||||
@@ -611,9 +611,9 @@ class TestBackendAbstraction:
|
||||
path = MEMPALACE_PKG / "backends" / "chroma.py"
|
||||
assert path.is_file(), "mempalace/backends/chroma.py does not exist."
|
||||
src = _read(path)
|
||||
assert (
|
||||
"BaseCollection" in src or "base" in src
|
||||
), "backends/chroma.py does not reference the base class."
|
||||
assert "BaseCollection" in src or "base" in src, (
|
||||
"backends/chroma.py does not reference the base class."
|
||||
)
|
||||
|
||||
def test_backends_importable(self):
|
||||
"""Both backend modules should be importable."""
|
||||
@@ -650,9 +650,9 @@ class TestI18n:
|
||||
def test_english_baseline_exists(self):
|
||||
"""en.json must exist as the baseline language file."""
|
||||
path = MEMPALACE_PKG / "i18n" / "en.json"
|
||||
assert (
|
||||
path.is_file()
|
||||
), "mempalace/i18n/en.json does not exist. English baseline is required."
|
||||
assert path.is_file(), (
|
||||
"mempalace/i18n/en.json does not exist. English baseline is required."
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -737,9 +737,9 @@ class TestReadmeToolCountConsistency:
|
||||
counts = re.findall(r"(\d+)\s+tools", readme)
|
||||
if len(counts) > 1:
|
||||
unique = set(counts)
|
||||
assert (
|
||||
len(unique) == 1
|
||||
), f"README mentions different tool counts: {counts}. All occurrences must agree."
|
||||
assert len(unique) == 1, (
|
||||
f"README mentions different tool counts: {counts}. All occurrences must agree."
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
+92
-3
@@ -1381,9 +1381,9 @@ def test_rebuild_index_runs_sqlite_preflight_before_chromadb_open(tmp_path, caps
|
||||
PAGE = 4096
|
||||
CORRUPT_BYTES = 16384 # 4 pages
|
||||
HEADER_GUARD = PAGE * 2 # leave header + root pages intact
|
||||
assert (
|
||||
pre_size >= HEADER_GUARD + CORRUPT_BYTES
|
||||
), f"sqlite db too small to mangle without truncating: {pre_size} bytes"
|
||||
assert pre_size >= HEADER_GUARD + CORRUPT_BYTES, (
|
||||
f"sqlite db too small to mangle without truncating: {pre_size} bytes"
|
||||
)
|
||||
# Round (pre_size - CORRUPT_BYTES) down to a page boundary so we
|
||||
# mangle whole pages. Cap at offset 40960 (page 10) for stable
|
||||
# diagnostics across SQLite versions that may grow the file.
|
||||
@@ -1855,3 +1855,92 @@ def test_rebuild_from_sqlite_honors_configured_drawer_collection_name(tmp_path,
|
||||
)
|
||||
except Exception:
|
||||
pass # Expected: collection wasn't created.
|
||||
|
||||
|
||||
# ── _vacuum_and_rebuild_fts5 ──────────────────────────────────────────
|
||||
|
||||
|
||||
def test_vacuum_and_rebuild_fts5_vacuums_and_rebuilds(tmp_path):
|
||||
"""VACUUM runs and FTS5 index is rebuilt when the table is present."""
|
||||
sqlite_path = tmp_path / "chroma.sqlite3"
|
||||
with closing(sqlite3.connect(str(sqlite_path))) as conn:
|
||||
conn.execute(
|
||||
"CREATE VIRTUAL TABLE embedding_fulltext_search"
|
||||
" USING fts5(string_value, tokenize='unicode61')"
|
||||
)
|
||||
conn.execute("INSERT INTO embedding_fulltext_search(string_value) VALUES('hello world')")
|
||||
conn.commit()
|
||||
|
||||
repair._vacuum_and_rebuild_fts5(str(tmp_path))
|
||||
|
||||
with closing(sqlite3.connect(str(sqlite_path))) as conn:
|
||||
result = conn.execute("PRAGMA integrity_check").fetchall()
|
||||
assert result == [("ok",)]
|
||||
|
||||
|
||||
def test_vacuum_and_rebuild_fts5_no_fts5_table(tmp_path):
|
||||
"""VACUUM runs without error when embedding_fulltext_search is absent."""
|
||||
sqlite_path = tmp_path / "chroma.sqlite3"
|
||||
with closing(sqlite3.connect(str(sqlite_path))) as conn:
|
||||
conn.execute("CREATE TABLE dummy (id INTEGER PRIMARY KEY)")
|
||||
conn.commit()
|
||||
|
||||
# Must not raise even without the FTS5 table.
|
||||
repair._vacuum_and_rebuild_fts5(str(tmp_path))
|
||||
|
||||
with closing(sqlite3.connect(str(sqlite_path))) as conn:
|
||||
result = conn.execute("PRAGMA integrity_check").fetchall()
|
||||
assert result == [("ok",)]
|
||||
|
||||
|
||||
def test_vacuum_and_rebuild_fts5_missing_sqlite(tmp_path):
|
||||
"""Silently skips when chroma.sqlite3 does not exist."""
|
||||
repair._vacuum_and_rebuild_fts5(str(tmp_path)) # no file — must not raise
|
||||
|
||||
|
||||
@patch("mempalace.repair.shutil")
|
||||
@patch("mempalace.repair.ChromaBackend")
|
||||
def test_rebuild_index_calls_vacuum(mock_backend_cls, mock_shutil, tmp_path):
|
||||
"""rebuild_index closes chroma handles then calls _vacuum_and_rebuild_fts5.
|
||||
|
||||
ChromaDB's PersistentClient holds an open connection to chroma.sqlite3;
|
||||
VACUUM requires an exclusive lock so _close_chroma_handles must be called
|
||||
before _vacuum_and_rebuild_fts5.
|
||||
"""
|
||||
sqlite_path = tmp_path / "chroma.sqlite3"
|
||||
with closing(sqlite3.connect(str(sqlite_path))) as conn:
|
||||
conn.execute("CREATE TABLE dummy(id INTEGER PRIMARY KEY)")
|
||||
conn.commit()
|
||||
|
||||
mock_col = MagicMock()
|
||||
mock_col.count.return_value = 1
|
||||
mock_col.get.return_value = {
|
||||
"ids": ["id1"],
|
||||
"documents": ["doc1"],
|
||||
"metadatas": [{"wing": "a"}],
|
||||
}
|
||||
mock_new_col = MagicMock()
|
||||
mock_new_col.count.return_value = 1
|
||||
mock_temp_col = MagicMock()
|
||||
mock_temp_col.count.return_value = 1
|
||||
mock_backend = _install_mock_backend(mock_backend_cls, mock_col)
|
||||
mock_backend.create_collection.side_effect = [mock_temp_col, mock_new_col]
|
||||
|
||||
call_order = []
|
||||
with (
|
||||
patch.object(
|
||||
repair, "_close_chroma_handles", side_effect=lambda *a, **kw: call_order.append("close")
|
||||
) as mock_close,
|
||||
patch.object(
|
||||
repair,
|
||||
"_vacuum_and_rebuild_fts5",
|
||||
side_effect=lambda *a, **kw: call_order.append("vacuum"),
|
||||
) as mock_vacuum,
|
||||
):
|
||||
repair.rebuild_index(palace_path=str(tmp_path))
|
||||
mock_close.assert_called_once()
|
||||
mock_vacuum.assert_called_once()
|
||||
assert call_order == ["close", "vacuum"], "backend must be closed before VACUUM"
|
||||
args, kwargs = mock_vacuum.call_args
|
||||
assert args[0] == str(tmp_path)
|
||||
assert "progress" in kwargs
|
||||
|
||||
@@ -33,12 +33,12 @@ class TestSaveHookAutoMines:
|
||||
# `--mode convos` so the convo miner runs (not the projects miner).
|
||||
assert "TRANSCRIPT_PATH" in src, "hook must read transcript_path"
|
||||
assert "mempalace mine" in src, "hook must invoke `mempalace mine`"
|
||||
assert (
|
||||
'dirname "$TRANSCRIPT_PATH"' in src
|
||||
), "hook must mine the transcript's parent directory"
|
||||
assert (
|
||||
"--mode convos" in src
|
||||
), "transcript mine must use --mode convos, not the projects miner"
|
||||
assert 'dirname "$TRANSCRIPT_PATH"' in src, (
|
||||
"hook must mine the transcript's parent directory"
|
||||
)
|
||||
assert "--mode convos" in src, (
|
||||
"transcript mine must use --mode convos, not the projects miner"
|
||||
)
|
||||
|
||||
def test_mempal_dir_default_not_empty(self):
|
||||
"""If MEMPAL_DIR is still used, it should have a sensible default,
|
||||
@@ -86,16 +86,16 @@ class TestShellHookTranscriptValidation:
|
||||
def test_save_hook_defines_and_uses_validator(self):
|
||||
src = self._strip_comments(self._hook_src("mempal_save_hook.sh"))
|
||||
assert "is_valid_transcript_path() {" in src, "validator function must be defined"
|
||||
assert (
|
||||
'is_valid_transcript_path "$TRANSCRIPT_PATH"' in src
|
||||
), "validator must be invoked against TRANSCRIPT_PATH before mining"
|
||||
assert 'is_valid_transcript_path "$TRANSCRIPT_PATH"' in src, (
|
||||
"validator must be invoked against TRANSCRIPT_PATH before mining"
|
||||
)
|
||||
|
||||
def test_precompact_hook_defines_and_uses_validator(self):
|
||||
src = self._strip_comments(self._hook_src("mempal_precompact_hook.sh"))
|
||||
assert "is_valid_transcript_path() {" in src, "validator function must be defined"
|
||||
assert (
|
||||
'is_valid_transcript_path "$TRANSCRIPT_PATH"' in src
|
||||
), "validator must be invoked against TRANSCRIPT_PATH before mining"
|
||||
assert 'is_valid_transcript_path "$TRANSCRIPT_PATH"' in src, (
|
||||
"validator must be invoked against TRANSCRIPT_PATH before mining"
|
||||
)
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform == "win32",
|
||||
@@ -112,9 +112,7 @@ class TestShellHookTranscriptValidation:
|
||||
end = src.index("\n}\n", start) + 2
|
||||
func_src = src[start:end]
|
||||
script = tmp_path / "v.sh"
|
||||
script.write_text(
|
||||
f"{func_src}\n" 'is_valid_transcript_path "$1" && echo OK || echo NO\n'
|
||||
)
|
||||
script.write_text(f'{func_src}\nis_valid_transcript_path "$1" && echo OK || echo NO\n')
|
||||
|
||||
def run(arg: str) -> str:
|
||||
return subprocess.run(
|
||||
|
||||
@@ -184,12 +184,11 @@ class TestSearchMemories:
|
||||
|
||||
# Invariants on every hit.
|
||||
for h in hits:
|
||||
assert (
|
||||
0.0 <= h["similarity"] <= 1.0
|
||||
), f"similarity out of range: {h['similarity']} for {h['source_file']}"
|
||||
assert 0.0 <= h["similarity"] <= 1.0, (
|
||||
f"similarity out of range: {h['similarity']} for {h['source_file']}"
|
||||
)
|
||||
assert 0.0 <= h["effective_distance"] <= 2.0, (
|
||||
f"effective_distance out of range: {h['effective_distance']} "
|
||||
f"for {h['source_file']}"
|
||||
f"effective_distance out of range: {h['effective_distance']} for {h['source_file']}"
|
||||
)
|
||||
|
||||
# With the clamp, the closet-boosted a.md still ranks ahead of b.md —
|
||||
@@ -338,9 +337,9 @@ class TestSearchCLI:
|
||||
captured = capsys.readouterr()
|
||||
first_block, _, _ = captured.out.partition("[2]")
|
||||
# Lexical match must rank first
|
||||
assert (
|
||||
"b.md" in first_block
|
||||
), f"expected lexical match 'b.md' at rank 1, got:\n{captured.out}"
|
||||
assert "b.md" in first_block, (
|
||||
f"expected lexical match 'b.md' at rank 1, got:\n{captured.out}"
|
||||
)
|
||||
# Non-zero bm25 reported
|
||||
assert "bm25=" in first_block
|
||||
assert "bm25=0.0" not in first_block
|
||||
|
||||
@@ -97,9 +97,9 @@ class TestSweeperParsing:
|
||||
records = list(parse_claude_jsonl(str(mock_claude_jsonl)))
|
||||
assistant_rec = records[1]
|
||||
assert assistant_rec["role"] == "assistant"
|
||||
assert (
|
||||
"Paris" in assistant_rec["content"]
|
||||
), f"Assistant content blocks must be flattened to text; got: {assistant_rec['content']!r}"
|
||||
assert "Paris" in assistant_rec["content"], (
|
||||
f"Assistant content blocks must be flattened to text; got: {assistant_rec['content']!r}"
|
||||
)
|
||||
|
||||
def test_parse_preserves_tool_blocks_verbatim(self, tmp_path):
|
||||
"""Per the design principle "verbatim always", tool_use and
|
||||
|
||||
+30
-30
@@ -444,9 +444,9 @@ class TestSyncPalace:
|
||||
# Allow-list — params must be exactly the documented audit shape so
|
||||
# any future leak (source_file, content, ID lists, etc.) trips a
|
||||
# test failure rather than slipping through a deny-list.
|
||||
assert set(params.keys()) <= {
|
||||
"first_id"
|
||||
}, f"WAL params drifted from the audit allow-list: {params.keys()}"
|
||||
assert set(params.keys()) <= {"first_id"}, (
|
||||
f"WAL params drifted from the audit allow-list: {params.keys()}"
|
||||
)
|
||||
|
||||
def test_registry_sentinels_preserved_on_apply(self, tmp_dir, palace_path):
|
||||
"""F2 regression: convo miner `_reg_*` sentinels must survive sync apply.
|
||||
@@ -567,9 +567,9 @@ class TestSyncPalace:
|
||||
inner_resolved = inner.resolve(strict=False)
|
||||
outer_resolved = outer.resolve(strict=False)
|
||||
assert inner_resolved in roots, f"expected inner in roots, got {roots}"
|
||||
assert (
|
||||
outer_resolved not in roots
|
||||
), f"deepest should win exclusively: roots={roots}, outer leaked"
|
||||
assert outer_resolved not in roots, (
|
||||
f"deepest should win exclusively: roots={roots}, outer leaked"
|
||||
)
|
||||
|
||||
def test_apply_with_empty_project_dirs_raises(self, palace_path):
|
||||
"""Round-2 P1: `project_dirs=[]` (empty list) with apply must raise,
|
||||
@@ -608,9 +608,9 @@ class TestSyncPalace:
|
||||
project_dirs=[synced_world["repo_path"]],
|
||||
dry_run=False,
|
||||
)
|
||||
assert any(
|
||||
"Closet purge skipped" in record.getMessage() for record in caplog.records
|
||||
), f"expected closet-skip warning, got: {[r.getMessage() for r in caplog.records]}"
|
||||
assert any("Closet purge skipped" in record.getMessage() for record in caplog.records), (
|
||||
f"expected closet-skip warning, got: {[r.getMessage() for r in caplog.records]}"
|
||||
)
|
||||
|
||||
def test_metadata_cache_cleared_on_exception(self, monkeypatch, config, synced_world, kg):
|
||||
"""F9 regression: tool_sync's try/finally must clear `_metadata_cache`
|
||||
@@ -651,9 +651,9 @@ class TestSyncPalace:
|
||||
assert result.get("success") is False
|
||||
assert "simulated" in result.get("error", "")
|
||||
|
||||
assert (
|
||||
mcp_server._metadata_cache is None
|
||||
), "F9: cache must be cleared even when sync_palace raises"
|
||||
assert mcp_server._metadata_cache is None, (
|
||||
"F9: cache must be cleared even when sync_palace raises"
|
||||
)
|
||||
|
||||
def test_sync_report_keys_stable(self, synced_world):
|
||||
"""Regression: SyncReport schema must not silently drop a field."""
|
||||
@@ -946,9 +946,9 @@ class TestSyncPalace:
|
||||
wing="demo",
|
||||
dry_run=True,
|
||||
)
|
||||
assert (
|
||||
report["gitignored"] == 1
|
||||
), f"symmetric resolve broken: drawer mis-bucketed; report={report}"
|
||||
assert report["gitignored"] == 1, (
|
||||
f"symmetric resolve broken: drawer mis-bucketed; report={report}"
|
||||
)
|
||||
assert report["out_of_scope"] == 0
|
||||
|
||||
def test_classification_cache_avoids_redundant_disk_hits(
|
||||
@@ -1005,9 +1005,9 @@ class TestSyncPalace:
|
||||
)
|
||||
assert report["scanned"] == 5
|
||||
assert report["gitignored"] == 5
|
||||
assert (
|
||||
call_count["n"] == 1
|
||||
), f"cache miss: expected 1 _classify_drawer call (4 cache hits), got {call_count['n']}"
|
||||
assert call_count["n"] == 1, (
|
||||
f"cache miss: expected 1 _classify_drawer call (4 cache hits), got {call_count['n']}"
|
||||
)
|
||||
|
||||
def test_closet_batch_purge_single_call(self, synced_world, monkeypatch):
|
||||
"""Batched $in closet purge: one delete() call across all removable
|
||||
@@ -1074,16 +1074,16 @@ class TestSyncPalace:
|
||||
str(repo_path / "deleted.py"),
|
||||
}
|
||||
expected = len(seeded_sources & set(report["by_source"].keys()))
|
||||
assert (
|
||||
report["removed_closets"] == expected
|
||||
), f"removed_closets ({report['removed_closets']}) != |seeded ∩ removable| ({expected})"
|
||||
assert report["removed_closets"] == expected, (
|
||||
f"removed_closets ({report['removed_closets']}) != |seeded ∩ removable| ({expected})"
|
||||
)
|
||||
assert "wrapper" in captured, "get_closets_collection patch not invoked"
|
||||
assert (
|
||||
captured["wrapper"].delete_calls == 1
|
||||
), f"expected one batch delete call, got {captured['wrapper'].delete_calls}"
|
||||
assert (
|
||||
captured["wrapper"].get_calls == 1
|
||||
), f"expected one batch get call, got {captured['wrapper'].get_calls}"
|
||||
assert captured["wrapper"].delete_calls == 1, (
|
||||
f"expected one batch delete call, got {captured['wrapper'].delete_calls}"
|
||||
)
|
||||
assert captured["wrapper"].get_calls == 1, (
|
||||
f"expected one batch get call, got {captured['wrapper'].get_calls}"
|
||||
)
|
||||
|
||||
def test_registry_check_runs_before_cache_lookup(self, tmp_dir, palace_path):
|
||||
"""A non-registry drawer with the same source_file must NOT poison
|
||||
@@ -1148,9 +1148,9 @@ class TestSyncPalace:
|
||||
finally:
|
||||
del client
|
||||
assert "a_regular" not in survivors
|
||||
assert (
|
||||
"_reg_zzz_sentinel" in survivors
|
||||
), "registry sentinel was incorrectly pruned via cached non-registry verdict"
|
||||
assert "_reg_zzz_sentinel" in survivors, (
|
||||
"registry sentinel was incorrectly pruned via cached non-registry verdict"
|
||||
)
|
||||
|
||||
def test_normalize_project_dirs_sort_stable_on_equal_length(self):
|
||||
"""`_normalize_project_dirs` must sort by `(-len, str)` so equal-length
|
||||
|
||||
@@ -70,7 +70,7 @@ def main():
|
||||
]
|
||||
out.write("\n".join(header))
|
||||
for ts, role, text in turns:
|
||||
out.write(f"\n[{ts}] {role.upper()}\n{text}\n\n{'-'*72}\n")
|
||||
out.write(f"\n[{ts}] {role.upper()}\n{text}\n\n{'-' * 72}\n")
|
||||
if out is not sys.stdout:
|
||||
out.close()
|
||||
print(f"Wrote {len(turns)} turns to {sys.argv[2]}")
|
||||
|
||||
@@ -1224,7 +1224,7 @@ requires-dist = [
|
||||
{ name = "pytest", marker = "extra == 'dev'", specifier = ">=7.0" },
|
||||
{ name = "pytest-cov", marker = "extra == 'dev'", specifier = ">=4.0" },
|
||||
{ name = "pyyaml", specifier = ">=6.0,<7" },
|
||||
{ name = "ruff", marker = "extra == 'dev'", specifier = ">=0.4.0" },
|
||||
{ name = "ruff", marker = "extra == 'dev'", specifier = "==0.15.9" },
|
||||
{ name = "tomli", marker = "python_full_version < '3.11'", specifier = ">=2.0.0" },
|
||||
]
|
||||
provides-extras = ["dev", "spellcheck", "gpu", "dml", "coreml"]
|
||||
@@ -1234,7 +1234,7 @@ dev = [
|
||||
{ name = "psutil", specifier = ">=5.9" },
|
||||
{ name = "pytest", specifier = ">=7.0" },
|
||||
{ name = "pytest-cov", specifier = ">=4.0" },
|
||||
{ name = "ruff", specifier = ">=0.4.0" },
|
||||
{ name = "ruff", specifier = "==0.15.9" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
Reference in New Issue
Block a user