From 22ed63c3185cc5d860b9ebde8457b7e3e87553d3 Mon Sep 17 00:00:00 2001 From: itdove Date: Tue, 8 Sep 2026 14:02:13 -0400 Subject: [PATCH] DocRepoIngester: parallelize LLM extraction across files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Codex](https://github.com/openai/codex) Co-Authored-By: Codex (gpt-5.6-luna) --- src/lore/ingest/doc_repo.py | 29 ++++++++++--- tests/unit/test_ingest_doc_repo.py | 70 +++++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 12 deletions(-) diff --git a/src/lore/ingest/doc_repo.py b/src/lore/ingest/doc_repo.py index b3250d2..658b4df 100644 --- a/src/lore/ingest/doc_repo.py +++ b/src/lore/ingest/doc_repo.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +from concurrent.futures import ThreadPoolExecutor from pathlib import Path from lore.ingest.chunker import SUPPORTED_EXTENSIONS @@ -9,6 +10,7 @@ from lore.sync.parser import ParsedFile, compute_content_hash log = logging.getLogger("lore.ingest") +DEFAULT_MAX_WORKERS = 4 class DocRepoIngester: @@ -17,10 +19,15 @@ def __init__( provider: LLMProvider, doc_paths: list[str] | None = None, exclude_paths: list[str] | None = None, + max_workers: int = DEFAULT_MAX_WORKERS, ) -> None: + if max_workers < 1: + raise ValueError("max_workers must be at least 1") + self._provider = provider self._doc_paths = doc_paths self._exclude_paths = exclude_paths or [] + self._max_workers = max_workers def scan_files(self, repo_path: Path) -> list[Path]: candidates: list[Path] = [] @@ -89,11 +96,21 @@ def process_file( return parsed_files def scan_and_process(self, repo_path: Path) -> list[ParsedFile]: - files = self.scan_files(repo_path) + files = sorted( + self.scan_files(repo_path), + key=lambda path: path.relative_to(repo_path).as_posix(), + ) result: list[ParsedFile] = [] - for f in files: - try: - result.extend(self.process_file(f, repo_path)) - except Exception: - log.warning("Failed to process %s", f, exc_info=True) + + with ThreadPoolExecutor(max_workers=self._max_workers) as executor: + futures = [ + (file_path, executor.submit(self.process_file, file_path, repo_path)) + for file_path in files + ] + for file_path, future in futures: + try: + result.extend(future.result()) + except Exception: + log.warning("Failed to process %s", file_path, exc_info=True) + return result diff --git a/tests/unit/test_ingest_doc_repo.py b/tests/unit/test_ingest_doc_repo.py index 49dd03d..41d0a3a 100644 --- a/tests/unit/test_ingest_doc_repo.py +++ b/tests/unit/test_ingest_doc_repo.py @@ -1,5 +1,6 @@ from __future__ import annotations +import threading from pathlib import Path from lore.ingest.doc_repo import DocRepoIngester @@ -214,17 +215,74 @@ def test_scan_and_process_end_to_end(tmp_path): assert "docs/ref.md" in file_paths +def test_scan_and_process_is_parallel_and_deterministic(tmp_path): + for name in ("c.md", "a.md", "b.md"): + _write_doc(tmp_path / name) + + barrier = threading.Barrier(2) + lock = threading.Lock() + active = 0 + max_active = 0 + + class _ConcurrentProvider(_MockProvider): + def extract_from_chunk(self, chunk_text, heading, source_file): + nonlocal active, max_active + with lock: + active += 1 + max_active = max(max_active, active) + try: + if Path(source_file).name in {"a.md", "b.md"}: + barrier.wait(timeout=2) + return [DocChunkExtraction(key="guide:entry", summary="Entry.")] + finally: + with lock: + active -= 1 + + ingester = DocRepoIngester( + _ConcurrentProvider(), + doc_paths=["c.md", "a.md", "b.md"], + max_workers=2, + ) + result = ingester.scan_and_process(tmp_path) + + assert max_active >= 2 + assert [parsed.file_path for parsed in result] == ["a.md", "b.md", "c.md"] + + +def test_scan_and_process_continues_after_file_failure(tmp_path, monkeypatch): + _write_doc(tmp_path / "bad.md") + _write_doc(tmp_path / "good.md") + ingester = DocRepoIngester(_MockProvider(), max_workers=2) + + def process_file(file_path, repo_path): + if file_path.name == "bad.md": + raise RuntimeError("failed file") + return [ + ParsedFile( + key="guide:good", + value="Good.", + tags=None, + locked=False, + created_by=None, + projects=None, + content_hash="sha256:test", + file_path=file_path.relative_to(repo_path).as_posix(), + ) + ] + + monkeypatch.setattr(ingester, "process_file", process_file) + + result = ingester.scan_and_process(tmp_path) + + assert [parsed.file_path for parsed in result] == ["good.md"] + + def test_scan_and_process_handles_errors(tmp_path): _write_doc(tmp_path / "good.md") class _FailProvider(_MockProvider): - def __init__(self): - super().__init__() - self._calls = 0 - def extract_from_chunk(self, chunk_text, heading, source_file): - self._calls += 1 - if self._calls == 1: + if Path(source_file).name == "good.md": raise RuntimeError("LLM error") return [DocChunkExtraction(key="guide:ok", summary="OK.")]