Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

20.8 KB
Raw Blame
"""RouterBrain (Phase 5, CL-6) — main Jarvis tier router.

Orthogonal to `dodaios.brain.intent_router` (fast/deep/code, provider level).
`RouterBrain` classifies action targets (trivial / direct_action /
spawn_worker) IMPLICITLY via tool choice — no separate LLM call.

Design (plan §"Router-Design"):
- Haiku 4.5 / Gemini Flash as provider (via `BrainManager.from_tier_config("router")`).
- Delegation tool: ``spawn_worker``; direct actions use the explicitly registered
  router tools such as ``bash`` and ``screenshot``.
- Strict rule: the user utterance is NEVER rephrased; for `direct_action` and
  `spawn_worker` the utterance is passed VERBATIM as the tool argument.

Classification via tool choice:
- TRIVIAL    → brain responds directly (no tool call).
- DIRECT     → brain calls an explicitly registered non-spawn router tool.
- SPAWN      → brain calls `spawn_worker(utterance=...)`.

The loop (text stream + tool use) runs in `BrainDispatcher`; `RouterBrain`
remains a thin wrapper plus system-prompt injection.
"""
from __future__ import annotations

import base64
import logging
import time
from collections.abc import AsyncIterator
from typing import TYPE_CHECKING
from uuid import UUID, uuid4

from dodaios.core.bus import EventBus
from dodaios.core.config import JarvisConfig
from dodaios.core.events import AnnouncementRequested, VisionInjected
from dodaios.core.protocols import (
    BrainDelta,
    BrainMessage,
    BrainRequest,
    ImageBlock,
    Observation,
    Tool,
)
from dodaios.memory import CoreMemory, PersonStore, RecallStore, Soul, UserProfile
from dodaios.safety.tool_executor import ToolExecutor

from .ack_generator import generate_ack, is_voice_control_utterance
from .manager import BrainManager

if TYPE_CHECKING:
    from dodaios.brain.healthcheck import BrainConfigError  # noqa: F401 — re-raised
    from dodaios.vision.context_provider import VisionContextProvider


log = logging.getLogger(__name__)


# Router system prompt — the DISPATCH contract, and nothing else.
#
# Cut 2026-10-19 from ~4.4k characters to the two facts the model cannot know.
# Everything else is enforced or stated elsewhere (do NOT re-add it here):
#
# - Voice, tone, address, spoken-input continuity (sound-alike names) and the
#   prompt-injection rule → dodaios/brain/DODAIOS_PERSONA.md.
# - Honesty, research-versus-action routing (search_web vs cli_*), delegation
#   and settings self-control → BrainManager._CORE_CONTRACT.
# - Which agents exist and what "agent" means → the lead card
#   (dodaios/society/lead_card.py).
# - "Never spawn without an explicit request" → dodaios/brain/spawn_gate.py;
#   "never drive the desktop for a research question" → dodaios/brain/cu_gate.py.
# - Tool arguments and when to call a tool → each tool's own declaration.
# - Filler openers / "Sir" / jargon → dodaios/brain/output_filter.py; unbacked
#   action claims → the mandated_tool_unfulfilled / unbacked_action_claim
#   backstops.
#
# The "Noted" sentence is a machine contract: dodaios/memory/wiki/voice_bridge.py
# looks for one of _ACK_KEYWORDS ("noted", "notiert", "anotado", "tomei nota", ...)
# anywhere in the reply. The words in brackets are that contract, not prose.
SYSTEM_PROMPT = (
    "ROUTER CONTRACT\n"
    "An attached image is context, not a task; without one you have not seen the screen "
    "(`screenshot` shows it).\n"
    'When the user only states a fact worth keeping, reply with "Noted" (or the reply '
    "language's word: Notiert, Anotado, Tomei nota) plus one short sentence; the memory "
    "pipeline keys on that word."
)

class RouterBrain:
    """Main Jarvis router: thin wrapper around `BrainManager` in the router tier.

    The three categories (trivial / direct_action / spawn_worker) are
    decided IMPLICITLY via tool choice — the dispatcher tool-use loop in
    `BrainManager` handles the rest. This class itself contains no
    classification logic; that lives in `SYSTEM_PROMPT`.
    """

    def __init__(
        self,
        config: JarvisConfig,
        bus: EventBus,
        *,
        tools: dict[str, Tool],
        tool_executor: ToolExecutor,
        core_memory: CoreMemory | None = None,
        recall: RecallStore | None = None,
        user_profile: UserProfile | None = None,
        soul: Soul | None = None,
        people: PersonStore | None = None,
        vision_provider: VisionContextProvider | None = None,
    ) -> None:
        self._bus = bus
        self._vision = vision_provider
        self._manager = BrainManager.from_tier_config(
            "router",
            config=config,
            bus=bus,
            tools=tools,
            tool_executor=tool_executor,
            core_memory=core_memory,
            recall=recall,
            user_profile=user_profile,
            soul=soul,
            people=people,
        )
        # The router-specific system prompt is appended in `_build_system_prompt`
        # before the core contract. The assistant's name is in the identity block.
        self._manager._system_prompt_extra = SYSTEM_PROMPT

    @property
    def manager(self) -> BrainManager:
        """Access to the underlying BrainManager (for tests/debug)."""
        return self._manager

    @property
    def active_provider(self) -> str:
        return self._manager.active_provider

    @property
    def tools(self) -> dict[str, Tool]:
        return self._manager._tools

    @property
    def system_prompt_extra(self) -> str:
        return self._manager._system_prompt_extra

    # ------------------------------------------------------------------
    # Perceived-latency acknowledgment hook
    # ------------------------------------------------------------------

    def _output_locale(self, utterance: str) -> str:
        """The turn's output language, via the ONE resolver.

        Anything Jarvis says to the user (clarifying questions, the tool ack,
        the completion marker) resolves through ``resolve_output_language``,
        never a local de/en heuristic, which honours
        the ``brain.reply_language`` pin, conversation stickiness, and every
        supported locale equally.
        """
        try:
            from dodaios.core.config import load_config  # noqa: PLC0415
            from dodaios.core.turn_language import (  # noqa: PLC0415
                DEFAULT_LOCALE,
                resolve_output_language,
            )

            cfg = load_config()
            pin = str(getattr(cfg.brain, "reply_language", "") or "")
            stt_language = str(getattr(cfg.stt, "language", "") or "")
            return resolve_output_language(
                pin, stt_language, utterance, default=DEFAULT_LOCALE
            )
        except Exception:  # noqa: BLE001 — never fail a turn over a language probe
            log.debug("router: output-locale resolution failed", exc_info=True)
            from dodaios.core.turn_language import DEFAULT_LOCALE  # noqa: PLC0415

            return DEFAULT_LOCALE

    def _build_ack_emitter(self, utterance: str):
        """Construct the async callback that publishes ``AnnouncementRequested``.

        Returns ``None`` when there is no bus to publish on, or when the
        utterance is a Voice-Control command (skip-category 3 from the
        dropdown spec).

        The emitter:

        * resolves the ack template via ``ack_generator.generate_ack`` using
          the language picked from the utterance;
        * suppresses the announcement entirely when the generator returns
          ``None`` (skip-list — passive reads, low-latency UI events);
        * publishes with ``priority="normal"`` so it queues behind any
          higher-priority interrupt without barging in itself.
        """
        if self._bus is None:
            return None
        if is_voice_control_utterance(utterance):
            return None
        bus = self._bus
        language = self._output_locale(utterance)

        async def emit(tool_name: str, tool_args: dict) -> None:
            text = generate_ack(tool_name, tool_args, language=language)
            if text is None:
                return
            await bus.publish(
                AnnouncementRequested(
                    text=text,
                    priority="normal",
                    language=language,
                    source_layer="brain.router.ack",
                )
            )

        return emit

    # ------------------------------------------------------------------
    # Streaming-Entrypoint
    # ------------------------------------------------------------------

    async def handle(
        self,
        utterance: str,
        *,
        history: list[BrainMessage] | None = None,
        trace_id: UUID | None = None,
        conversation_id: str | None = None,
    ) -> AsyncIterator[BrainDelta]:
        """Processes a user utterance and streams `BrainDelta` chunks.

        Tool use happens implicitly in the dispatcher. For TRIVIAL turns the
        brain streams text chunks; for DIRECT_ACTION / SPAWN_WORKER the
        dispatcher runs the tool-use loop and yields the final text responses
        after the tool result.

        Args:
            utterance: User text, verbatim (not rephrased).
            history: Optional message history (default: empty — the router is
                typically stateless per turn).
            trace_id: Optional for flight-recorder correlation.
        """
        brain = self._manager._get_brain(
            self._manager.active_provider,
            self._manager._fast_model(self._manager.active_provider),
        )
        images: tuple[ImageBlock, ...] = ()
        screen_note = ""

        # --- Screen Context: the user explicitly asked Jarvis to LOOK --------
        #
        # Runs BEFORE the permanent-vision path and takes precedence over it,
        # because it answers a stricter question with a better answer: it
        # captures the monitor the CURSOR is on (permanent vision follows the
        # foreground window, which is a different screen whenever the user
        # reads one display while typing on another), it redacts secure fields
        # before the pixels leave the process, and it never persists them.
        #
        # It is additive, not a replacement: `should_attach_screenshot` below
        # also fires on on-screen ACTION turns ("click the button"), which are
        # not look-requests but still need an image, so that path stays.
        #
        # An AMBIGUOUS turn ends here with a question instead of a capture —
        # falling through would attach an image while asking whether to look
        # at one. A PRIVACY refusal likewise ends the turn and shuts the path
        # below, because falling through there would photograph the exact
        # window the user's rule protects. A TECHNICAL failure (no display, no
        # permission) also ends honestly: a pure look must never turn into a
        # Computer-Use action just because the capture backend is unavailable.
        turn_trace_id = trace_id or uuid4()
        screen = await self._manager._resolve_screen_context_turn(
            utterance,
            source_layer="brain.router.handle",
            conversation_id=conversation_id,
            allow_voice_confirm=False,
            trace_id=turn_trace_id,
        )
        if screen.ends_the_turn:
            spoken = screen.question or screen.message or ""
            if spoken:
                log.info(
                    "screen_context: turn ended without capture (status=%s)",
                    screen.status,
                )
                yield BrainDelta(content=spoken)
                yield BrainDelta(finish_reason="stop")
                return
        elif screen.has_image:
            import base64 as _base64  # noqa: PLC0415

            images = (
                ImageBlock(
                    mime=screen.mime,
                    data_b64=_base64.b64encode(screen.image or b"").decode("ascii"),
                    source_hash=screen.source_hash,
                ),
            )
            screen_note = screen.note
            log.info("screen_context: %s", screen.receipt)

        # Permanent vision: inject a fresh screen observation as an ImageBlock
        # when the provider is available and not paused. Errors are not fatal
        # — the text-only fallback keeps the conversation running. Skipped
        # entirely when Screen Context already supplied an image above.
        vision_none = self._vision is None
        paused = (
            bool(getattr(self._vision, "is_paused", False))
            if self._vision is not None
            else None
        )
        log.info(
            "Vision-Inject Diagnose: path=RouterBrain vision_none=%s "
            "is_paused=%s brain_provider=%s",
            vision_none,
            paused,
            self._manager.active_provider,
        )
        # Same attach-on-reference gate as BrainManager._collect_vision_images:
        # only inject the screenshot when the utterance clearly refers to the
        # screen, so this path matches the SCREEN-CONTEXT prompt and does not
        # bury the conversation under an unrequested image. O(1) regex, no LLM
        # call (AP-9: never add latency on the voice path).
        from dodaios.brain.vision_gate import should_attach_screenshot

        if (
            not images
            and not screen.blocks_other_screen_paths
            and self._vision is not None
            and not self._vision.is_paused
            and should_attach_screenshot(utterance)
        ):
            try:
                obs = await self._vision.current()
                hash_prefix = (obs.screenshot_hash or "")[:16]
                geometry = tuple(
                    getattr(obs, "monitor_geom", (0, 0, 0, 0))
                    or (0, 0, 0, 0)
                )
                width, height = (
                    (int(geometry[2]), int(geometry[3]))
                    if len(geometry) >= 4
                    else (0, 0)
                )
                capture_age_ms = max(
                    0, int((time.time_ns() - obs.timestamp_ns) / 1_000_000)
                )
                log.info(
                    "Vision-Inject Observation: screenshot_hash=%s "
                    "dimensions=%dx%d capture_age_ms=%d",
                    hash_prefix,
                    width,
                    height,
                    capture_age_ms,
                )
                mime, image_b64 = await _read_observation_image_b64(obs)
                log.info(
                    "Vision-Inject encoded: brain_provider=%s mime=%s "
                    "screenshot_hash=%s len_image_b64=%d",
                    self._manager.active_provider,
                    mime,
                    hash_prefix,
                    len(image_b64),
                )
                images = (
                    ImageBlock(
                        mime=mime,
                        data_b64=image_b64,
                        source_hash=obs.screenshot_hash,
                    ),
                )
                if self._bus is not None:
                    bytes_size = len(image_b64) * 3 // 4
                    age_ms = int((time.time_ns() - obs.timestamp_ns) / 1_000_000)
                    await self._bus.publish(VisionInjected(
                        trace_id=turn_trace_id,
                        screenshot_hash=obs.screenshot_hash,
                        bytes_size=bytes_size,
                        capture_age_ms=age_ms,
                    ))
            except Exception as exc:  # noqa: BLE001
                # Laut loggen: Silent Text-Only-Fallback hat uns in Prod stumm
                # gemacht (User merkt nicht, dass Jarvis den Screen verloren
                # hat). exc_info=True schreibt Stacktrace in den Flight-Recorder.
                log.error(
                    "Vision-Inject fehlgeschlagen (%s) — Text-Only Fallback. "
                    "Pruefe ob VisionContextProvider.start() gelaufen ist und "
                    "data/flight_recorder/blobs/ beschreibbar ist.",
                    exc,
                    exc_info=True,
                )

        messages: list[BrainMessage] = list(history or [])
        messages.append(
            BrainMessage(
                role="user",
                content=f"{screen_note}\n\n{utterance}" if screen_note else utterance,
                images=images,
            )
        )

        # A successful one-shot look is evidence-only. Build the dispatcher
        # without tools so neither Computer-Use nor any other action can be
        # selected from untrusted pixels. The production BrainManager already
        # enforces this boundary; RouterBrain must enforce the same contract.
        dispatcher = self._manager._build_dispatcher(
            brain, tools_override={} if screen.has_image else None
        )
        tools_payload = dispatcher.tools_payload()
        system_prompt = self._manager._build_system_prompt()

        if self._manager._tools and self._manager._tool_executor is not None:
            # The tool-use loop aggregates internally; we yield the final
            # aggregate as a single delta (stream-compatible adapter). This
            # gives the caller a uniform AsyncIterator regardless of whether
            # a tool call or plain text was produced.
            ack_emitter = self._build_ack_emitter(utterance)
            # ``turn_context`` rather than prefixing the utterance: the raw
            # utterance is what every downstream gate matches on (cu_gate,
            # spawn_gate, voice-control), and prefixing it with a description
            # full of words like "window" and "screen" would quietly widen
            # those gates. This channel reaches the model without touching them.
            agg = await dispatcher.dispatch(
                utterance,
                images=images,
                history=history,
                trace_id=turn_trace_id,
                ack_emitter=ack_emitter,
                turn_context=screen_note,
            )
            # Perceived-latency completion marker. The user opted for an
            # unconditional "Erledigt." at the end of any turn that
            # actually executed tools — even if the brain's own response
            # already carries the substance, the trailing marker signals
            # "task done" cleanly. Trivial-path turns (no tool_calls) skip
            # this; Voice-Control utterances skip too (action == confirmation).
            final_text = agg.text or ""
            # A turn that only looked up which tool exists did nothing to
            # report as done.
            acted = any(tc.get("name") != "find_tools" for tc in agg.tool_calls)
            if acted and not is_voice_control_utterance(utterance):
                from .ack_generator import final_summary_marker
                lang = self._output_locale(utterance)
                marker = final_summary_marker(language=lang)
                if final_text.strip():
                    final_text = final_text.rstrip().rstrip(".") + ". " + marker
                else:
                    final_text = marker
            if final_text:
                yield BrainDelta(content=final_text)
            for tc in agg.tool_calls:
                yield BrainDelta(tool_call=tc)
            if agg.finish_reason:
                yield BrainDelta(
                    finish_reason=agg.finish_reason,
                    usage=agg.usage or None,
                )
            return

        # Simple mode: no tool executor — stream directly (images are already
        # included in the user BrainMessage above).
        req = BrainRequest(
            messages=tuple(messages),
            tools=tuple(tools_payload),
            system=system_prompt,
            stream=True,
        )
        async for delta in brain.complete(req):
            yield delta


async def _read_observation_image_b64(obs: Observation) -> tuple[str, str]:
    """Reads `Observation.screenshot_path` as a Base64-encoded image.

    Uses `asyncio.to_thread` for file I/O so the event loop is not blocked.
    If the observation has no path (e.g. from pure ui_tree mode), a
    `ValueError` is raised and the caller falls back to text-only.
    """
    import asyncio

    if obs.screenshot_path is None:
        raise ValueError("Observation ohne screenshot_path")
    path = obs.screenshot_path

    def _read() -> bytes:
        with open(path, "rb") as fh:
            return fh.read()

    data = await asyncio.to_thread(_read)
    return _detect_image_mime(data), base64.b64encode(data).decode("ascii")


def _detect_image_mime(data: bytes) -> str:
    """Determines the MIME type for the provider adapters."""
    if data.startswith(b"\xff\xd8\xff"):
        return "image/jpeg"
    if data.startswith(b"\x89PNG\r\n\x1a\n"):
        return "image/png"
    raise ValueError("The observation is not in a supported image format")


async def _read_observation_png_b64(obs: Observation) -> str:
    """Backwards-compatible helper for old tests/callsites."""
    mime, data_b64 = await _read_observation_image_b64(obs)
    if mime != "image/png":
        raise ValueError(f"The observation is {mime}, not image/png")
    return data_b64