Skip to content

armgit / LEANN

Public Imported from GitHub
0

[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.

feat: add `leann watch` command, line numbers in code chunks, and fix suppressed search output (#242)

Yichuan Wang49bc4d1435e5
diff --git a/.gitignore b/.gitignore
index e60379d..8254b65 100755
--- a/.gitignore
+++ b/.gitignore
@@ -109,3 +109,5 @@ apps/multimodal/vision-based-pdf-multi-vector/multi-vector-colpali-native-weavia
# AUR build directory (Arch Linux)
paru-bin/
+merkle-tree-test/
+*.pickle
diff --git a/README.md b/README.md
index b1a642b..4903fcd 100755
--- a/README.md
+++ b/README.md
@@ -1082,6 +1082,9 @@ leann ask my-docs --interactive
# Ask a single question (non-interactive)
leann ask my-docs "Where are prompts configured?"
+# Detect file changes since last build/watch checkpoint
+leann watch my-docs
+
# List all your indexes
leann list
@@ -1093,6 +1096,7 @@ leann remove my-docs
- Auto-detects document formats (PDF, TXT, MD, DOCX, PPTX + code files)
- **🧠 AST-aware chunking** for Python, Java, C#, TypeScript files
- Smart text chunking with overlap for all other content
+- **📂 File change detection** via Merkle tree snapshots (`leann watch`)
- Multiple LLM providers (Ollama, OpenAI, HuggingFace)
- Organized index storage in `.leann/indexes/` (project-local)
- Support for advanced search parameters
@@ -1100,7 +1104,7 @@ leann remove my-docs
<details>
<summary><strong>📋 Click to expand: Complete CLI Reference</strong></summary>
-You can use `leann --help`, or `leann build --help`, `leann search --help`, `leann ask --help`, `leann list --help`, `leann remove --help` to get the complete CLI reference.
+You can use `leann --help`, or `leann build --help`, `leann search --help`, `leann watch --help`, `leann ask --help`, `leann list --help`, `leann remove --help` to get the complete CLI reference.
**Build Command:**
```bash
@@ -1127,6 +1131,24 @@ Options:
--pruning-strategy {global,local,proportional}
```
+**Watch Command:**
+```bash
+leann watch INDEX_NAME
+
+# Compares the current file system state against the last checkpoint (Merkle tree snapshot)
+# and reports which files have been added, removed, or modified, along with their chunk IDs.
+#
+# - Automatically saves a new checkpoint after detecting changes
+# - Each subsequent run compares against the most recent checkpoint
+# - File change detection uses SHA-256 content hashing via a Merkle tree
+#
+# Example output:
+# === Changes since last checkpoint ===
+# modified (1):
+# - /path/to/file.py
+# chunks: 42, 43, 44
+```
+
**Ask Command:**
```bash
leann ask INDEX_NAME [OPTIONS]
diff --git a/packages/leann-core/src/leann/chunking_utils.py b/packages/leann-core/src/leann/chunking_utils.py
index 90b425c..1f083a2 100644
--- a/packages/leann-core/src/leann/chunking_utils.py
+++ b/packages/leann-core/src/leann/chunking_utils.py
@@ -274,7 +274,13 @@ def create_ast_chunks(
# Merge document metadata + astchunk metadata
combined_metadata = {**doc_metadata, **astchunk_metadata}
- all_chunks.append({"text": chunk_text.strip(), "metadata": combined_metadata})
+ # Trim partial first line left by overlap
+ stripped = chunk_text.strip()
+ if stripped and not stripped[0].isdigit():
+ first_nl = stripped.find("\n")
+ if first_nl != -1:
+ stripped = stripped[first_nl + 1 :]
+ all_chunks.append({"text": stripped, "metadata": combined_metadata})
logger.info(
f"Created {len(chunks)} AST chunks from {language} file: {doc.metadata.get('file_name', 'unknown')}"
@@ -385,6 +391,17 @@ def create_text_chunks(
all_chunks = []
if use_ast_chunking:
code_docs, text_docs = detect_code_files(documents, local_code_extensions)
+ # Prepend line numbers to code documents for navigation
+ if code_docs:
+ from llama_index.core.schema import MediaResource
+
+ for doc in code_docs:
+ original = doc.get_content()
+ lines = original.split("\n")
+ w = len(str(len(lines)))
+ doc.text_resource = MediaResource(
+ text="\n".join(f"{i + 1:>{w}}|{line}" for i, line in enumerate(lines))
+ )
if code_docs:
try:
all_chunks.extend(
diff --git a/packages/leann-core/src/leann/cli.py b/packages/leann-core/src/leann/cli.py
index 6d56669..d4d2907 100644
--- a/packages/leann-core/src/leann/cli.py
+++ b/packages/leann-core/src/leann/cli.py
@@ -1,7 +1,10 @@
import argparse
import asyncio
import contextlib
+import io
+import json
import os
+import sys
import time
from pathlib import Path
from typing import Any, Optional, Union
@@ -19,36 +22,65 @@ from .settings import (
resolve_openai_api_key,
resolve_openai_base_url,
)
+from .sync import FileSynchronizer
@contextlib.contextmanager
def suppress_cpp_output(suppress: bool = True):
- """Context manager to suppress C++ stdout/stderr output from FAISS/HNSW.
-
- This redirects file descriptors at the OS level to suppress native C++ output
- that cannot be controlled via Python's logging framework.
+ """Context manager to suppress C++ stdout/stderr output from FAISS/HNSW
+ while preserving Python print() output.
+
+ C++ native code writes directly to OS file descriptors (fd 1 / fd 2).
+ Python print() goes through sys.stdout / sys.stderr, which are Python
+ file objects. We redirect the OS fds to /dev/null (silencing C++) but
+ point sys.stdout / sys.stderr at copies of the *original* fds so that
+ Python output still reaches the terminal.
"""
if not suppress:
yield
return
- # Save original file descriptors
- old_stdout_fd = os.dup(1)
- old_stderr_fd = os.dup(2)
+ # 1. Duplicate the original OS file descriptors
+ saved_stdout_fd = os.dup(1)
+ saved_stderr_fd = os.dup(2)
+
+ # 2. Build Python file objects that write to the saved (real) fds.
+ # closefd=False so closing these wrappers won't close the duped fds.
+ py_stdout = io.TextIOWrapper(
+ io.FileIO(saved_stdout_fd, mode="w", closefd=False), encoding=sys.stdout.encoding or "utf-8"
+ )
+ py_stderr = io.TextIOWrapper(
+ io.FileIO(saved_stderr_fd, mode="w", closefd=False), encoding=sys.stderr.encoding or "utf-8"
+ )
+
+ old_sys_stdout = sys.stdout
+ old_sys_stderr = sys.stderr
try:
- # Open /dev/null for writing
+ # 3. Redirect OS-level fds to /dev/null → silences C++ output
devnull = os.open(os.devnull, os.O_WRONLY)
- os.dup2(devnull, 1) # Redirect stdout
- os.dup2(devnull, 2) # Redirect stderr
+ os.dup2(devnull, 1)
+ os.dup2(devnull, 2)
os.close(devnull)
+
+ # 4. Point Python's sys.stdout/stderr at the real terminal
+ sys.stdout = py_stdout
+ sys.stderr = py_stderr
+
yield
finally:
- # Restore original file descriptors
- os.dup2(old_stdout_fd, 1)
- os.dup2(old_stderr_fd, 2)
- os.close(old_stdout_fd)
- os.close(old_stderr_fd)
+ # 5. Restore everything
+ # Flush wrappers first (they still need the saved fds to be open)
+ py_stdout.flush()
+ py_stderr.flush()
+
+ sys.stdout = old_sys_stdout
+ sys.stderr = old_sys_stderr
+
+ os.dup2(saved_stdout_fd, 1)
+ os.dup2(saved_stderr_fd, 2)
+ os.close(saved_stdout_fd)
+ os.close(saved_stderr_fd)
def extract_pdf_text_with_pymupdf(file_path: str) -> str | None:
@@ -290,6 +322,13 @@ Examples:
help="Fall back to traditional chunking if AST chunking fails (default: True)",
)
+ # Watch command
+ watch_parser = subparsers.add_parser(
+ "watch",
+ help="Compare current files against last checkpoint and report changes",
+ )
+ watch_parser.add_argument("index_name", help="Index name")
+
# Search command
search_parser = subparsers.add_parser("search", help="Search documents")
search_parser.add_argument("index_name", help="Index name")
@@ -1431,6 +1470,16 @@ Examples:
file_path = doc.metadata.get("file_path", "")
is_code_file = any(source_path.endswith(ext) for ext in code_file_exts)
+ # For code files, prepend line numbers so chunks carry them
+ if is_code_file:
+ from llama_index.core.schema import MediaResource
+
+ original_text = doc.get_content()
+ lines = original_text.split("\n")
+ width = len(str(len(lines)))
+ numbered = "\n".join(f"{i + 1:>{width}}|{line}" for i, line in enumerate(lines))
+ doc.text_resource = MediaResource(text=numbered)
+
# Extract metadata to preserve with chunks
chunk_metadata = {
"file_path": file_path or source_path,
@@ -1449,11 +1498,133 @@ Examples:
nodes = parser.get_nodes_from_documents([doc])
for node in nodes:
- all_texts.append({"text": node.get_content(), "metadata": chunk_metadata})
+ text = node.get_content()
+ # For code chunks, trim a partial first line left by overlap
+ # (a valid line starts with digits followed by '|')
+ if is_code_file and text and not text[0].isdigit():
+ first_nl = text.find("\n")
+ if first_nl != -1:
+ text = text[first_nl + 1 :]
+ all_texts.append({"text": text, "metadata": chunk_metadata})
print(f"Loaded {len(documents)} documents, {len(all_texts)} chunks")
return all_texts
+ def _parse_file_types(self, custom_file_types: Optional[str]) -> Optional[list[str]]:
+ if not custom_file_types:
+ return None
+ extensions = [ext.strip() for ext in custom_file_types.split(",") if ext.strip()]
+ return [ext if ext.startswith(".") else f".{ext}" for ext in extensions]
+
+ def _sync_ignore_patterns(self, include_hidden: bool) -> Optional[list[str]]:
+ if include_hidden:
+ return None
+ return ["**/.*"]
+
+ def _resolve_sync_roots(self, docs_paths: list[str]) -> list[str]:
+ roots: set[str] = set()
+ for path in docs_paths:
+ path_obj = Path(path).resolve()
+ if path_obj.is_dir():
+ roots.add(str(path_obj))
+ elif path_obj.is_file():
+ roots.add(str(path_obj.parent))
+ return sorted(roots)
+
+ def _initialize_file_synchronizers(
+ self,
+ roots: list[str],
+ include_extensions: Optional[list[str]],
+ ignore_patterns: Optional[list[str]],
+ ) -> None:
+ for root in roots:
+ try:
+ FileSynchronizer(
+ root_dir=root,
+ ignore_patterns=ignore_patterns,
+ include_extensions=include_extensions,
+ auto_load=True,
+ )
+ except Exception as exc:
+ print(f"⚠️ Failed to initialize file synchronizer for {root}: {exc}")
+
+ def _write_sync_config(
+ self,
+ index_dir: Path,
+ roots: list[str],
+ include_extensions: Optional[list[str]],
+ ignore_patterns: Optional[list[str]],
+ ) -> None:
+ sync_config_path = index_dir / "sync_roots.json"
+ config = {
+ "roots": roots,
+ "include_extensions": include_extensions,
+ "ignore_patterns": ignore_patterns,
+ }
+ with open(sync_config_path, "w", encoding="utf-8") as f:
+ json.dump(config, f, indent=2)
+
+ def _resolve_index_for_watch(self, index_name: str) -> Optional[dict[str, Path]]:
+ if self.index_exists(index_name):
+ index_dir = self.indexes_dir / index_name
+ passages_file = index_dir / "documents.leann.passages.jsonl"
+ return {"index_dir": index_dir, "passages_file": passages_file}
+
+ all_matches = self._find_all_matching_indexes(index_name)
+ if not all_matches:
+ print(
+ f"Index '{index_name}' not found. Use 'leann build {index_name} --docs <dir> [<dir2> ...]' to create it."
+ )
+ return None
+
+ if len(all_matches) == 1:
+ match = all_matches[0]
+ else:
+ current_matches = [m for m in all_matches if m.get("is_current")]
+ match = current_matches[0] if current_matches else all_matches[0]
+ location_desc = (
+ "current project"
+ if match.get("is_current")
+ else f"project '{match['project_path'].name}'"
+ )
+ print(
+ f"Found {len(all_matches)} indexes named '{index_name}', using index from {location_desc}"
+ )
+
+ if match.get("kind") == "cli":
+ index_dir = match["index_dir"]
+ passages_file = index_dir / "documents.leann.passages.jsonl"
+ else:
+ index_dir = match["meta_file"].parent
+ file_base = match["file_base"]
+ passages_file = index_dir / f"{file_base}.passages.jsonl"
+
+ return {"index_dir": index_dir, "passages_file": passages_file}
+
+ def _load_chunk_ids_by_file(self, passages_file: Path) -> dict[str, list[str]]:
+ chunk_ids_by_file: dict[str, list[str]] = {}
+ with open(passages_file, encoding="utf-8") as f:
+ for line in f:
+ line = line.strip()
+ if not line:
+ continue
+ try:
+ data = json.loads(line)
+ except json.JSONDecodeError:
+ continue
+ metadata = data.get("metadata") or {}
+ file_path = metadata.get("file_path") or metadata.get("source")
+ if not file_path:
+ continue
+ chunk_id = data.get("id")
+ if chunk_id is None:
+ continue
+ normalized_path = str(Path(file_path).resolve())
+ chunk_ids_by_file.setdefault(normalized_path, []).append(str(chunk_id))
+ if file_path != normalized_path:
+ chunk_ids_by_file.setdefault(file_path, []).append(str(chunk_id))
+ return chunk_ids_by_file
+
async def build_index(self, args):
docs_paths = args.docs
# Use current directory name if index_name not provided
@@ -1561,9 +1732,88 @@ Examples:
builder.build_index(index_path)
print(f"Index built at {index_path}")
+ sync_roots = self._resolve_sync_roots(docs_paths)
+ if sync_roots:
+ include_extensions = self._parse_file_types(args.file_types)
+ ignore_patterns = self._sync_ignore_patterns(args.include_hidden)
+ self._initialize_file_synchronizers(sync_roots, include_extensions, ignore_patterns)
+ self._write_sync_config(index_dir, sync_roots, include_extensions, ignore_patterns)
+
# Register this project directory in global registry
self.register_project_dir()
+ async def watch_index(self, args):
+ index_name = args.index_name
+ resolved = self._resolve_index_for_watch(index_name)
+ if not resolved:
+ return
+
+ index_dir = resolved["index_dir"]
+ passages_file = resolved["passages_file"]
+ sync_config_path = index_dir / "sync_roots.json"
+ if not sync_config_path.exists():
+ print(
+ f"Sync config not found for index '{index_name}'. Rebuild the index to enable watch."
+ )
+ return
+
+ with open(sync_config_path, encoding="utf-8") as f:
+ config = json.load(f)
+
+ roots = config.get("roots") or []
+ include_extensions = config.get("include_extensions")
+ ignore_patterns = config.get("ignore_patterns")
+ if not roots:
+ print(f"No sync roots found for index '{index_name}'.")
+ return
+ if not passages_file.exists():
+ print(f"Passages file not found: {passages_file}")
+ return
+
+ chunk_ids_by_file = self._load_chunk_ids_by_file(passages_file)
+
+ added_paths: set[str] = set()
+ removed_paths: set[str] = set()
+ modified_paths: set[str] = set()
+
+ for root in roots:
+ try:
+ fs = FileSynchronizer(
+ root_dir=root,
+ ignore_patterns=ignore_patterns,
+ include_extensions=include_extensions,
+ auto_load=True,
+ )
+ added, removed, modified = fs.check_for_changes()
+ except Exception as exc:
+ print(f"⚠️ Failed to check {root}: {exc}")
+ continue
+
+ added_paths.update(added)
+ removed_paths.update(removed)
+ modified_paths.update(modified)
+
+ if not added_paths and not removed_paths and not modified_paths:
+ print("No changes detected.")
+ else:
+ print("\n=== Changes since last checkpoint ===")
+ for label, paths in (
+ ("added", sorted(added_paths)),
+ ("removed", sorted(removed_paths)),
+ ("modified", sorted(modified_paths)),
+ ):
+ if not paths:
+ continue
+ print(f"\n{label} ({len(paths)}):")
+ for file_path in paths:
+ normalized_path = str(Path(file_path).resolve())
+ chunk_ids = chunk_ids_by_file.get(normalized_path) or []
+ if not chunk_ids:
+ chunk_ids = chunk_ids_by_file.get(file_path) or []
+ chunk_display = ", ".join(chunk_ids) if chunk_ids else "(not in index)"
+ print(f" - {file_path}")
+ print(f" chunks: {chunk_display}")
+
async def search_documents(self, args):
index_name = args.index_name
query = args.query
@@ -1893,6 +2143,8 @@ Examples:
elif args.command == "build":
with suppress_cpp_output(suppress):
await self.build_index(args)
+ elif args.command == "watch":
+ await self.watch_index(args)
elif args.command == "search":
with suppress_cpp_output(suppress):
await self.search_documents(args)
diff --git a/packages/leann-core/src/leann/sync.py b/packages/leann-core/src/leann/sync.py
index 1d5a0e0..fe3f875 100644
--- a/packages/leann-core/src/leann/sync.py
+++ b/packages/leann-core/src/leann/sync.py
@@ -18,6 +18,7 @@ def hash_data(data: str | bytes):
@dataclass
class MerkleTreeNode:
+ ## TODO: this merkle tree only has two layer, need to improve if we want to scale to large codebase
hash: str
data: str
children: dict[str, "MerkleTreeNode"] = field(default_factory=dict)
@@ -93,11 +94,13 @@ class FileSynchronizer:
recursive=True,
exclude=self.ignore_patterns,
required_exts=self.include_extensions,
- exclude_empty=True,
+ exclude_empty=False,
)
+ # print('reader.iter_data() length', len(list(reader.iter_data())))
for file in reader.iter_data():
if len(file) > 1:
+ # print('file length is greater than 1', file)
continue # SimpleDirectoryReader can load more than 1 documents for weird file types e.g. PDFs
file = file[0]
try: