11from __future__ import annotations
22
33import asyncio
4+ import json
45import logging
56import sys
67import time
78import traceback
89import uuid
10+ from typing import Callable , Optional
911
1012try :
1113 from google .adk .models import google_llm as _google_llm
@@ -25,6 +27,18 @@ def _truncate_for_log(text: str, max_len: int = 900) -> str:
2527 return f"{ text [:max_len ]} …<truncated len={ len (text )} >"
2628
2729
30+ def _find_publish_all_impl (runner ) -> Optional [Callable [[object ], dict ]]:
31+ agent = getattr (runner , "agent" , None )
32+ sub_agents = getattr (agent , "sub_agents" , None )
33+ if not isinstance (sub_agents , list ):
34+ return None
35+ for sub_agent in sub_agents :
36+ impl = getattr (sub_agent , "_publish_all_impl" , None )
37+ if callable (impl ):
38+ return impl
39+ return None
40+
41+
2842def main () -> int :
2943 logging .basicConfig (
3044 level = logging .INFO ,
@@ -208,6 +222,30 @@ def _thread_main() -> None:
208222 else :
209223 logging .warning ("[agent=poll_orchestrator] no final response produced." )
210224
225+ publish_called = any ("tool_call: publish_all" in call for call in tool_calls )
226+ publish_result_like = bool (final_text ) and all (
227+ marker in final_text for marker in ('"targets_count"' , '"published_count"' , '"x_posted_count"' )
228+ )
229+ if final_text and not publish_called and not publish_result_like :
230+ publish_impl = _find_publish_all_impl (runner )
231+ if publish_impl :
232+ logging .warning (
233+ "[main] publish_all was not called by model; triggering deterministic fallback publish."
234+ )
235+ try :
236+ publish_result = publish_impl (final_text )
237+ final_text = json .dumps (publish_result , ensure_ascii = False )
238+ tool_calls .append ("fallback_call: publish_all" )
239+ logging .info (
240+ "[main] fallback publish result: %s" ,
241+ _truncate_for_log (final_text ),
242+ )
243+ except Exception as publish_exc :
244+ logging .error ("[main] fallback publish failed: %s" , publish_exc )
245+ traceback .print_exc ()
246+ else :
247+ logging .error ("[main] publish_all fallback unavailable (implementation not found)." )
248+
211249 iteration_ok = bool (final_text )
212250 log_metric (
213251 "poll_agent.run_end" ,
0 commit comments