research_loop
research_loop
¶
Agentic research loop over the hybrid-search tool.
A small, self-contained planner-executor loop:
- the planner is supplied by the caller (the web endpoint resolves it from
config, falling back to
gemma4:31bon Ollama for legacy installs), - it can call :meth:
HybridSearch.searchover the corpus and — when the caller wires one — aweb_searchover the public web (22 Sept 2026: « Fais-moi une recherche [sur] des sites web qui proposent des jeux de programmation » under Deep Research got « your corpus has nothing », then a list from the model's memory with an invented site; the follow-up « Donne-moi les liens de ces sites » got links pulled from the user's e-mails — the request was stateless and web-less), - it receives the previous turns of the conversation (
history) so a follow-up like « ces sites » has a referent, - 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¶
WebHit
dataclass
¶
One public-web result, as the router's web_search callable returns it.
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 = "",
web_hits: List[WebHit] = list(),
error: str = "",
)
One tool call together with what the planner asked for and got.
tool_name is "search", "web_search" or "clarify". For
search calls, num_results, top_titles and raw_hits are
populated (web_hits for the web); 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,
web_search: Optional[WebSearcher] = 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:
|
web_search
|
Optional
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/diapason/agents/research_loop.py
Methods:¶
run
¶
run(
query: str,
*,
history: Optional[Sequence[Message]] = None,
hints: Optional[Sequence[str]] = None,
) -> ResearchResult
Run the loop end-to-end and return the synthesis plus a trace.
history — the previous user/assistant turns, placed before the
query so a follow-up has its referent. hints — system lines the
caller established about THIS query (what « ces sites » refers to,
that the user asked for the web), placed right after it.
Source code in src/diapason/agents/research_loop.py
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 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 | |
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/diapason/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/diapason/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.
Source code in src/diapason/agents/research_loop.py
build_web_sources_for_client
¶
build_web_sources_for_client(
hits: List[WebHit],
*,
ref_offset: int = 0,
total_cap: int = 10,
) -> List[Dict[str, Any]]
Same shape as :func:build_sources_for_client; source is web
and sender the site, so the client renders a web hit like any other.