fix(startup): lazy-init root_agent in lifespan — fix Cloud Run startup probe

This commit is contained in:
Chris Christiansen 2026-06-23 12:17:58 +00:00
parent 4dfcffd0b3
commit 0f663c1d3d
2 changed files with 4 additions and 4 deletions

View File

@ -263,9 +263,6 @@ def build_agent(mode: str = "light", message: str = "") -> tuple:
return agent, actual_model, instruction return agent, actual_model, instruction
root_agent, _, _ = build_agent(mode="light")
async def _run_async(message: str, user_id: str, session_id: str, mode: str, caller_type: str = "agent") -> str: async def _run_async(message: str, user_id: str, session_id: str, mode: str, caller_type: str = "agent") -> str:
mode = _normalize_mode(mode) mode = _normalize_mode(mode)
agent, actual_model, instruction = build_agent(mode=mode, message=message) agent, actual_model, instruction = build_agent(mode=mode, message=message)

View File

@ -21,7 +21,7 @@ try:
from google.adk.runners import Runner from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService from google.adk.sessions import InMemorySessionService
from google.genai.types import Content, Part from google.genai.types import Content, Part
from agent import root_agent from agent import build_agent
from agents.recommendations_engine import get_recommendations from agents.recommendations_engine import get_recommendations
from token_logger import create_bq_table_if_not_exists from token_logger import create_bq_table_if_not_exists
except ImportError as e: except ImportError as e:
@ -29,6 +29,7 @@ except ImportError as e:
raise raise
session_service = InMemorySessionService() session_service = InMemorySessionService()
root_agent = None
APP_NAME = os.environ.get("CLOUD_RUN_SERVICE", "gcp-orchestrator") APP_NAME = os.environ.get("CLOUD_RUN_SERVICE", "gcp-orchestrator")
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5") PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
BQ_BILLING_DATASET = os.environ.get("BQ_BILLING_DATASET", "billing_data") BQ_BILLING_DATASET = os.environ.get("BQ_BILLING_DATASET", "billing_data")
@ -42,9 +43,11 @@ _budget_store: dict = {}
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
global root_agent
logger.info(f"OSVauco agent '{APP_NAME}' starting up") logger.info(f"OSVauco agent '{APP_NAME}' starting up")
vertexai.init(project=PROJECT_ID, location=os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1")) vertexai.init(project=PROJECT_ID, location=os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1"))
create_bq_table_if_not_exists() create_bq_table_if_not_exists()
root_agent, _, _ = build_agent(mode="light")
logger.info(f"Static dir: {STATIC_DIR} (exists={STATIC_DIR.exists()})") logger.info(f"Static dir: {STATIC_DIR} (exists={STATIC_DIR.exists()})")
yield yield
logger.info(f"OSVauco agent '{APP_NAME}' shutting down") logger.info(f"OSVauco agent '{APP_NAME}' shutting down")