ForkFit Docs
ServerIntelligence

Graph & Orchestrator

Cognitive LangGraph state graph structure, node flow topology, and transactional graph execution state.

Graph & Orchestrator

The core execution engine of the ForkFit Intelligence Service utilizes LangGraph to compile a deterministic cyclic state graph of specialized nutritional agents.

Implementation: orchestrator.py and state.py


1. Graph Topology

The orchestrator compiles a StateGraph with parallel specialist nodes to optimize execution latency. It routes the execution from the Planner node, fanning out in parallel to four specialist agents: Safety, Nutrition, Budget, and Culture. Each of these specialist agents inspects state["required_agents"] at the start of execution and performs a fast no-op return if it is not required for the current run.

Once all parallel specialists finish, the graph fans in back to the Recipe agent. From the Recipe agent, execution proceeds sequentially (in serial) through the remaining chain: Recipe $\rightarrow$ Calendar $\rightarrow$ Shopping $\rightarrow$ Consensus $\rightarrow$ Judge.

Node Flow Diagram

Failed & attempts < max Passed or max attempts Entry Planner Safety Nutrition Budget Culture Recipe Calendar Shopping Consensus Judge judge_router Visualization END
  • Serial Orchestration Steps:

    • Recipe Formulation: Wires specialist inputs to select candidate recipes and alternatives.
    • Calendar Scheduling: Maps selected recipes into daily schedules aligned with sleep and workouts.
    • Shopping List Generation: Aggregates grocery requirements, deducting available pantry stocks.
    • Consensus Agreement: Resolves meal conflicts and outputs a unified plan payload.
    • QA Quality Judge: Validates final plans against caloric bounds, dietary restrictions, and safety profiles.
  • Compilation: Wires all specialist nodes, registers the conditional entry router, configures the Judge loops, and compiles the graph for async execution streaming.

  • Routing: The judge_router checks if the validation passes. If validation fails and the replan_count is less than max_replan_attempts (default 2), it routes back to the planner node. Otherwise, it proceeds to the visualization node.


2. Graph State definition

The single source of truth for variables flowing through the agents is GraphState.

GraphState TypedDict Fields

  • Input Fields:
    • user_id (str): Authenticated user ID.
    • prompt (str): Raw natural language instruction.
    • context (dict): Hydrated context containing active goals, biometrics, preferences, candidate recipes, and active memories.
    • chat_history (list[dict]): Conversational message history.
  • Planner Outputs:
    • required_agents (list[str]): Agents identified as required for this run.
    • execution_plan (str): Description of the reasoning/steps.
  • Specialist Outputs:
    • safety_result (SafetyResult): Safe vs. allergen-blocked recipe lists and warnings.
    • nutrition_result (NutritionResult): Computed macro splits and candidate recipe adherence scores.
    • budget_result (BudgetResult): Plan budget checks and LLM-suggested food substitutions.
    • culture_result (CultureResult): Aligned recipe list and cultural rejection reasons.
    • recipe_result (RecipeResult): Selected recipe blueprints and alternatives.
    • calendar_result (CalendarResult): Daily schedule plans with scheduled meals.
    • shopping_result (ShoppingResult): Grocery items list and estimated costs.
  • Consensus & Quality Assurance:
    • merged_plan (MergedPlan): Combined meal schedules, nutrition summary, and consolidated grocery list.
    • judge_verdict (JudgeVerdict): Boolean pass status, validation failures, and dynamic replan prompt instructions.
    • replan_count (int): Number of replanning loops executed.
  • UI Output:
    • ui_elements (list[UIElementSpecDict]): JSON specifications for frontend React charts/timelines.
    • final_text (str): Final markdown chat reply.
  • Injected Dependencies:
    • _pool (asyncpg.Pool): Database connection pool.
    • _settings (Settings): Global configuration flags.
    • _step_callback (Callable): Streams agent step latency/details over gRPC.
    • _token_callback (Callable): Streams real-time chat text delta tokens.

On this page