app
zocrates.agents.app ¶
Application assembly and lifecycle management for Zocrates multi-agent systems.
ROLE_DEFAULT_THINKING
module-attribute
¶
ROLE_DEFAULT_THINKING = {'coordinator': 'medium', 'library_search': 'low', 'library_synthesizer': 'high', 'online_search': 'low', 'online_synthesizer': 'high'}
ZOCRATES_COMPACTION_PROMPT_TEMPLATE
module-attribute
¶
ZOCRATES_COMPACTION_PROMPT_TEMPLATE = '## OBJECTIVE\nYou are compacting the prior research dialogue history between a researcher and Zocrates (an AI academic research assistant).\n\nYour objective is to generate an accurate, dense, and grounded synthesis/summary of past dialogue turns so future research turns maintain complete context.\n\n## INSTRUCTIONS\n\n### Sections\nYou summary must include the following sections:\n- Sources: List all papers, studies, and documents discussed in the conversation using unnumbered bullet points with authors, publication year, paper title, and journal/source (e.g. "- Smith et al. (2021) — Mechanisms of BRCA-mediated repair (Nature Genetics)"). Do NOT use numeric citation brackets ([1], [2]) or numbered lists for sources.\n- Key Findings & Established Decisions: Summarize core findings, established facts, methodologies, and user decisions in dense bullet points. Attribute findings directly to the authors (e.g. "Petersen et al. (2022) showed...").\n- Unresolved Questions & Next Steps: Note any explicit knowledge gaps, ongoing literature inquiries, user preferences, or next steps identified in the conversation.\n\n### Critical Guidelines:\n- Exclude Internal Mechanics: Do not describe internal tool calls, retrieval loops, or intermediate system steps; focus strictly on final evidence obtained and conceptual conclusions.\n- Conciseness & Dense Structure: Keep the summary dense, direct, and under 400 words. Use structured bullet points rather than verbose paragraphs.\n- Do NOT include conversational pleasantries, greeting filler, or language declarations. \n- Do NOT attempt to answer any user questions found in the history. Your task is strictly to summarize the past dialogue into the sections above.\n\n## CONVERSATION HISTORY TO SUMMARIZE\n\n<dialogue_history>\n\n{conversation_history}\n\n</dialogue_history>\n\n## SUMMARY\nNow, provide the dense, structured academic compaction summary of the dialogue history above:\n'
__all__
module-attribute
¶
__all__ = ['app', 'coordinator_agent', 'root_agent', 'set_agent_models', 'standalone_summarizer', 'get_compaction_summarizer', 'FailOpenLlmEventSummarizer', 'build_events_compaction_config', 'build_compaction_llm', 'ZOCRATES_COMPACTION_PROMPT_TEMPLATE']
app
module-attribute
¶
standalone_summarizer
module-attribute
¶
standalone_summarizer = FailOpenLlmEventSummarizer(llm=build_compaction_llm(load_zocrates_config()), prompt_template=ZOCRATES_COMPACTION_PROMPT_TEMPLATE)
FailOpenLlmEventSummarizer ¶
Bases: LlmEventSummarizer
Event summarizer with fail-open resilience and pipeline isolation scope filtering.
Initialize fail-open LLM event summarizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments forwarded to |
()
|
**kwargs
|
Any
|
Keyword arguments forwarded to |
{}
|
Source code in zocrates/agents/common/compaction.py
maybe_summarize_events
async
¶
Summarize conversation events with scope filtering and fail-open resilience.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
list[Event]
|
List of session events to consider for compaction summarization. |
required |
Returns:
| Type | Description |
|---|---|
Event | None
|
Compaction event containing the generated summary, or None if skipped or on failure. |
Source code in zocrates/agents/common/compaction.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
build_agent_llm ¶
Build a dedicated LiteLlm instance for an agent role.
Resolves configuration according to precedence
agent_cfg > global_cfg > defaults
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_cfg
|
dict[str, Any]
|
Role-specific override dict (e.g. from override_agent_models). |
required |
global_cfg
|
dict[str, Any] | None
|
Optional global configuration dictionary (loaded if None). |
None
|
zotero_data_dir
|
Path | str | None
|
Optional Zotero data directory path. |
None
|
Returns:
| Type | Description |
|---|---|
BaseLlm
|
Configured ADK BaseLlm (LiteLlm) instance. |
Source code in zocrates/llm.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
build_compaction_llm ¶
Build a fast, dedicated LiteLlm instance for context compaction with suppressed reasoning effort.
Source code in zocrates/agents/common/compaction.py
build_events_compaction_config ¶
Deactivated: native ADK pre-invocation triggers are replaced by post-turn adaptive compaction.
Source code in zocrates/agents/common/compaction.py
create_coordinator_agent ¶
create_coordinator_agent(model=None, thinking_level='medium', generate_content_config=None, search_library_tool=None, search_online_tool=None)
Create the root Coordinator agent.
The coordinator answers conversational turns directly and delegates
literature retrieval queries to the autonomous search pipeline(s).
model can be a single model or a dict specifying per-agent models.
Source code in zocrates/agents/coordinator/agent.py
get_agent_generate_content_config ¶
get_agent_generate_content_config(thinking_level=DEFAULT_THINKING_LEVEL, include_thoughts=True, thinking_budget=None, num_retries=None, zotero_data_dir=None, model=None)
Construct an ADK GenerateContentConfig with thinking/reasoning parameters.
Sets ADK's native thinking_config (for Google Gemini models) as well as http_options.extra_body (for LiteLLM / OpenRouter / OpenAI reasoning models) and http_options.retry_options for baked-in network retries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thinking_level
|
str | ThinkingLevel | None
|
Thinking level ('minimal', 'low', 'medium', 'high', or None/disabled). |
DEFAULT_THINKING_LEVEL
|
include_thoughts
|
bool
|
Whether thoughts are returned in the response stream. |
True
|
thinking_budget
|
int | None
|
Optional token budget for reasoning (int or None). |
None
|
num_retries
|
int | None
|
Optional maximum retry attempts for HTTP requests (defaults to llm_num_retries in config or DEFAULT_LLM_NUM_RETRIES = 4). |
None
|
zotero_data_dir
|
Path | str | None
|
Optional root Zotero data directory for loading config. |
None
|
model
|
str | Any | None
|
Optional model identifier or LLM instance to tailor reasoning schema and Ollama thinking gating. |
None
|
Returns:
| Type | Description |
|---|---|
GenerateContentConfig | None
|
Configured GenerateContentConfig, or None if thinking is disabled. |
Source code in zocrates/agents/common/config.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
get_compaction_summarizer ¶
get_llm_model ¶
Instantiate the active ADK BaseLlm instance based on Zocrates configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path | str | None
|
Optional path to Zotero data directory. |
None
|
Returns:
| Type | Description |
|---|---|
BaseLlm
|
Configured ADK BaseLlm (LiteLlm) instance. |
Source code in zocrates/llm.py
load_zocrates_config ¶
Load the Zocrates config from the data directory.
If zotero_data_dir is None, attempts to obtain it from the active
LibrarySyncer service or the ZOTERO_DATA_DIR environment variable.
Returns config merged with standard defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path | str | None
|
Root Zotero data directory. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Parsed JSON config dict merged with defaults. |
Source code in zocrates/config.py
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 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 | |
patch_adk_active_task_scope_rewind ¶
Patch ADK _find_active_task_scope to filter out rewound events before checking active scopes.
When an in-flight subagent pipeline (which stamps events with an isolation_scope, e.g. 'search_library_pipeline:xyz' or 'search_online_pipeline:xyz') is rewound via rewind_before_invocation_id, its unfinished isolation scope remains in session.events. ADK's native _find_active_task_scope walks session.events directly without taking rewinds into account, causing subsequent user turns to mistakenly be stamped with the dead subagent's isolation scope.
This patch ensures _find_active_task_scope evaluates active task scopes against the live events filtered by _apply_rewinds(session.events).
Source code in zocrates/agents/common/adk_compat.py
patch_adk_request_input_context_filter ¶
Patch ADK contents filter to exclude adk_request_input function calls/responses from agent LLM context.
In dynamic workflows with Human-In-The-Loop (HITL), RequestInput emits an Event
with a function call named 'adk_request_input' and the user resume response emits
a function response named 'adk_request_input'. While ADK natively filters out
internal framework events like 'adk_framework' and 'adk_request_credential' from
being included in an agent's LLM context, it omits 'adk_request_input'. This causes
ADK's _fencing module to wrap the checkpoint call and response in cross-agent
fencing (<<
This patch ensures 'adk_request_input' events are filtered out from LLM context generation, preserving clean continuation prompts for subagents.
Source code in zocrates/agents/common/adk_compat.py
patch_compaction_subagent_isolation ¶
Ensure token compaction only executes for the root coordinator agent, never for subagents, and eliminate duplicate post-invocation compaction runs.
In multi-agent systems, subagents (e.g. library_search_agent, online_search_agent) execute inside isolated pipelines. If token-threshold compaction runs inside subagents, it mistakenly compacts the parent session's history before every internal tool call, causing massive latency overhead and redundant compaction cycles.
Additionally, Google ADK 2.8.0 contains an upstream context propagation bug where
BaseAgent._create_invocation_context performs a shallow model_copy() of InvocationContext,
causing token_compaction_checked = True to only be set on the child context and never reaching
the root context in runners.py. This causes runners.py to pass skip_token_compaction=False to
_run_post_invocation_compaction, triggering a second, completely redundant compaction cycle.
This patch links parent contexts and guards _run_compaction_for_token_threshold so compaction
never executes more than once per turn.
Source code in zocrates/agents/common/adk_compat.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
set_agent_models ¶
Propagate updated LLM model(s) across coordinator, subagents, and compaction summarizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_model
|
Any
|
BaseLlm instance, model name string, or dict mapping role keys to models. If None, re-instantiates default model via get_llm_model(). |
None
|
zotero_data_dir
|
Any
|
Optional Zotero data directory path for config loading. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The updated model or model configuration. |
Source code in zocrates/agents/app.py
set_coordinator_agent_models ¶
Propagate updated LLM model(s) and content configs across coordinator and subagents.
Resolves 1:1 canonical role keys (coordinator, library_search, library_synthesizer, online_search, online_synthesizer).