research_loop
research_loop
¶
Agentic research loop over the hybrid-search tool.
A small, self-contained planner-executor loop:
- the planner is a local Ollama chat model (default
gemma4:31b), - the only tool it can call is :meth:
HybridSearch.search, - it gets up to
max_iterationstool calls, - tool results are trimmed before re-entering the context window, and
- the final reply must cite specific hits.
The loop is deliberately decoupled from the rest of the agent scaffolding
(ToolUsingAgent, EventBus, AgentContext, etc.) so the surface stays
small. Anything that wants tracing or registry integration can wrap it.
Classes¶
ToolInvocation
dataclass
¶
ToolInvocation(arguments: Dict[str, Any], num_results: int = 0, top_titles: List[str] = list(), raw_hits: List[SearchHit] = list(), tool_name: str = 'search', response: str = '')
One tool call together with what the planner asked for and got.
tool_name is "search" or "clarify". For search calls,
num_results, top_titles and raw_hits are populated; for
clarify calls, response holds the user's answer.
ResearchAgent
¶
ResearchAgent(engine: InferenceEngine, search: HybridSearch, *, model: str = DEFAULT_PLANNER_MODEL, max_iterations: int = 5, temperature: float = 0.3, max_tokens: int = 1500, num_ctx: int = 16384, clarify_handler: Optional[Callable[[str], str]] = None, on_event: Optional[Callable[[Dict[str, Any]], None]] = None, available_sources: Optional[List[str]] = None)
Planner + executor loop over a single hybrid-search tool.
| PARAMETER | DESCRIPTION |
|---|---|
engine
|
An
TYPE:
|
search
|
The HybridSearch instance the planner can call.
TYPE:
|
model
|
Planner model tag (default
TYPE:
|
max_iterations
|
Hard ceiling on tool calls before the loop is forced into synthesis.
TYPE:
|
temperature
|
Generation parameters passed through to
TYPE:
|
max_tokens
|
Generation parameters passed through to
TYPE:
|
num_ctx
|
Generation parameters passed through to
TYPE:
|
on_event
|
Optional callback fired at loop milestones so callers (e.g. the SSE
research router) can stream progress without rewriting the loop.
Receives a dict in one of these shapes:
-
TYPE:
|
Source code in src/openjarvis/agents/research_loop.py
Functions¶
run
¶
Run the loop end-to-end and return the synthesis plus a trace.
Source code in src/openjarvis/agents/research_loop.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
Functions¶
shape_results_for_model
¶
shape_results_for_model(hits: List[SearchHit], *, detailed_top: int = 5, thread_ctx_per_hit: int = 3, total_cap: int = 20, ref_offset: int = 0) -> Dict[str, Any]
Compact a hit list into a JSON payload the planner can chew through.
The first detailed_top rows keep their content snippet and trimmed
thread context; the remainder are summarised to title + sender + date so
the planner still sees the breadth of what's available without blowing the
context window. Each hit gets a numeric ref (1-indexed, plus
ref_offset) so the synthesis can cite it as [N]. The offset lets
multi-search runs hand the planner globally unique refs across calls so
a later renumbering pass can dedupe by first appearance.
Source code in src/openjarvis/agents/research_loop.py
renumber_citations
¶
renumber_citations(text: str, ref_to_source: Dict[int, Dict[str, Any]]) -> Tuple[str, List[Dict[str, Any]]]
Renumber [N] citations in text by first-appearance order.
The planner sees globally-offset refs across multiple search calls
(search 1 returns 1..20, search 2 returns 21..40, …). When the
synthesis arrives, the first ref the model actually cited becomes
[1], the second unique one becomes [2], and so on. Repeats
map to the same new ref. Refs the synthesis never cites are dropped
from the returned sources list — only the ones the user can
actually click on get carried through.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Synthesis text containing inline
TYPE:
|
ref_to_source
|
Mapping from the original (offset) ref to the source dict that
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
(new_text, ordered_sources)
|
|
Source code in src/openjarvis/agents/research_loop.py
build_sources_for_client
¶
build_sources_for_client(hits: List[SearchHit], *, total_cap: int = 20, ref_offset: int = 0) -> List[Dict[str, Any]]
Produce the citation-friendly sources list streamed to the frontend.
One entry per hit, in the same order the planner sees them — so a
[N] citation in the synthesis maps to sources[N - 1] on the
client. We don't deduplicate by document_id: separate chunks of the
same email each get their own citation slot since the planner may quote
different parts.