Skip to content

Enable parallel gather_evidence with concurrency controls#1340

Open
vermakov-edison wants to merge 6 commits into
mainfrom
feature/parallel-gather-evidence
Open

Enable parallel gather_evidence with concurrency controls#1340
vermakov-edison wants to merge 6 commits into
mainfrom
feature/parallel-gather-evidence

Conversation

@vermakov-edison

Copy link
Copy Markdown
Collaborator

Summary

  • Make gather_evidence safe to invoke concurrently by passing sub-questions via evidence_question instead of temporarily mutating session.question, and set GatherEvidence.CONCURRENCY_SAFE = True.
  • Add coordination in Docs.aget_evidence: per-Docs texts-index build lock (pickle-safe, stored outside the model), optional session_lock for serializing session merges, and optional process-wide evidence_global_max_concurrent semaphore for LLM throttling.
  • Refactor GatherEvidence into _gather_evidence_for_question (supports per-call settings/partitioning overrides for downstream consumers) and add test_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.question during each call, which races when multiple gather_evidence invocations 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_evidence
  • Full agent test suite (tests/test_agents.py)
  • Existing Docs / evidence tests (tests/test_paperqa.py)

Made with Cursor

vermakov-edison and others added 2 commits June 25, 2026 10:41
…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>
Copilot AI review requested due to automatic review settings June 26, 2026 20:01
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Jun 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GatherEvidence to pass sub-questions via evidence_question and marks it CONCURRENCY_SAFE = True.
  • Adds concurrency primitives in Docs.aget_evidence (texts-index build lock, optional session_lock, optional global semaphore driven by AnswerSettings.evidence_global_max_concurrent).
  • Adds an async test covering parallel gather_evidence calls.

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.

Comment thread src/paperqa/docs.py
Comment thread src/paperqa/docs.py Outdated
Comment thread tests/test_agents.py
pre-commit-ci-lite Bot and others added 4 commits June 26, 2026 20:05
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants