Enable parallel gather_evidence with concurrency controls#1340
Open
vermakov-edison wants to merge 6 commits into
Open
Enable parallel gather_evidence with concurrency controls#1340vermakov-edison wants to merge 6 commits into
vermakov-edison wants to merge 6 commits into
Conversation
…ks and global semaphore. Enhance `Docs` and `GatherEvidence` classes to support parallel evidence retrieval while preserving session state. Add tests for parallel execution of `gather_evidence` to ensure functionality and integrity.
Avoid duplicated map_fxn_summary and merge_results branches when global or session concurrency controls are disabled. Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes gather_evidence safe to run concurrently by avoiding mutation of session.question, and introduces coordination controls in Docs.aget_evidence (per-Docs index build lock, optional session-merge lock, and an optional global semaphore to cap concurrent evidence-summary LLM calls).
Changes:
- Refactors
GatherEvidenceto pass sub-questions viaevidence_questionand marks itCONCURRENCY_SAFE = True. - Adds concurrency primitives in
Docs.aget_evidence(texts-index build lock, optionalsession_lock, optional global semaphore driven byAnswerSettings.evidence_global_max_concurrent). - Adds an async test covering parallel
gather_evidencecalls.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| tests/test_agents.py | Adds a parallel-invocation regression test for gather_evidence. |
| src/paperqa/settings.py | Introduces AnswerSettings.evidence_global_max_concurrent to optionally throttle concurrent evidence-summary calls across tasks. |
| src/paperqa/docs.py | Adds concurrency coordination (index-build lock, session merge lock, optional global semaphore) and passes evidence_question into retrieval/summarization. |
| src/paperqa/agents/tools.py | Makes GatherEvidence concurrency-safe by removing session question swapping and serializing merges with a private lock. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Remove partitioning_fn from agent-facing gather_evidence, make session merge sync, document the new setting in README, and update tool schema test expectations. Co-authored-by: Cursor <cursoragent@cursor.com>
Replace claude-3-haiku-20240307 with claude-haiku-4-5 in the debug config and gemini/gemini-2.0-flash-lite with gemini/gemini-2.5-flash-lite in agent tests, since the old models return 404 (no longer available). Co-authored-by: Cursor <cursoragent@cursor.com>
Replace the wall-clock asyncio.sleep with an asyncio.Barrier so the two concurrent invocations are guaranteed to overlap regardless of CI load. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
gather_evidencesafe to invoke concurrently by passing sub-questions viaevidence_questioninstead of temporarily mutatingsession.question, and setGatherEvidence.CONCURRENCY_SAFE = True.Docs.aget_evidence: per-Docstexts-index build lock (pickle-safe, stored outside the model), optionalsession_lockfor serializing session merges, and optional process-wideevidence_global_max_concurrentsemaphore for LLM throttling.GatherEvidenceinto_gather_evidence_for_question(supports per-call settings/partitioning overrides for downstream consumers) and addtest_parallel_gather_evidence.Motivation
Agents often decompose a main question into sub-questions and want to gather evidence in parallel. The previous implementation swapped
session.questionduring each call, which races when multiplegather_evidenceinvocations overlap. This branch decouples the main session question from per-call evidence queries and serializes only the short session merge phase.API changes (backward compatible)
Docs.aget_evidence(..., evidence_question: str | None = None, session_lock: asyncio.Lock | None = None)AnswerSettings.evidence_global_max_concurrent: int | None = None(default unchanged behavior)EnvironmentState._evidence_lock(private, for agent use)Test plan
uv run pytest tests/test_agents.py::test_parallel_gather_evidencetests/test_agents.py)Docs/ evidence tests (tests/test_paperqa.py)Made with Cursor