"""Graph state shared by the main graph and every pack subgraph.""" from __future__ import annotations import operator from typing import Annotated, Any, TypedDict from pydantic import BaseModel, Field class Event(BaseModel): """Normalized input that starts a run (Phase 5 persists these in an inbox table).""" type: str # "chat.message", "demo.refund_request", "schedule.tick", ... source: str = "chat" # chat | webhook | email | whatsapp | cron | manual text: str | None = None # free text; trusted only when ``trusted`` is True payload: dict[str, Any] = Field(default_factory=dict) trusted: bool = False # True only for authenticated operator input # Phase 13 (filled by core.i18n.pipeline at run start; ``text`` keeps the original): language: str | None = None # en | ur | roman_ur | other | und language_confidence: float | None = None normalized_text: str | None = None # spelling/digit-normalized ``text``; still untrusted class RunState(TypedDict, total=False): tenant_id: str run_id: str thread_id: str event: dict[str, Any] intent: str | None plan: list[dict[str, Any]] # supervisor steps: [{"intent": ..., "instruction": ...}] cursor: int extracted: dict[str, Any] # values from the quarantine extractor (all tainted) facts: dict[str, Any] # values read from the system of record (trusted) log: Annotated[list[str], operator.add] # operator-facing progress lines actions: Annotated[list[dict[str, Any]], operator.add] # gateway outcomes, in order status: str # running | completed | rejected | failed | unsupported output: str | None error: str | None # Phase 15: read-only, non-secret tenant snapshot set by the router node (ADR-0009 G5; # see core.agent.tenant_config) and heuristic signals on the inbound text (routing hints # only, never permissions; see core.observability.injection). tenant_config: dict[str, Any] signals: dict[str, Any] class StepInput(TypedDict, total=False): """What a pack subgraph receives: RunState without the accumulated ``log``/``actions``. Subgraphs then return only the entries they added, so the parent's ``operator.add`` reducers never double-count earlier steps. """ tenant_id: str run_id: str thread_id: str event: dict[str, Any] intent: str | None plan: list[dict[str, Any]] cursor: int extracted: dict[str, Any] facts: dict[str, Any] status: str output: str | None error: str | None tenant_config: dict[str, Any] # Phase 15 (G5) signals: dict[str, Any] # Phase 15 def subgraph_builder() -> Any: """``StateGraph`` preconfigured for pack subgraphs. Always use this in packs.""" from langgraph.graph import StateGraph return StateGraph(RunState, input_schema=StepInput)