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.

4.1 KB
Raw Blame
"""Shared CLI-brain prompt context extraction.

Regression guard for the live bug 2026-06-21: an English voice request was
answered in German (and spoken with an English TTS voice). Root cause — the
subscription-CLI brains (antigravity/agy, codex) rebuild their own flattened
prompt and dropped the authoritative reply-language directive that the
BrainManager appends LAST to the system prompt. The model then never learned
which language to answer in and anchored to the German persona ("Schef ...").
``extract_reply_language_directive`` re-surfaces that trailing directive so the
single authoritative output-language decision still reaches the CLI model.
"""
from __future__ import annotations

from dodaios.plugins.brain.cli_prompt_context import (
    extract_reply_language_directive,
    render_cli_standing_instructions,
)

_MANDATORY = (
    "REPLY LANGUAGE — MANDATORY: Always reply in English, no matter which "
    "language the user writes or speaks in. This overrides any other language "
    "cue anywhere in this prompt."
)


def _system_with_directive() -> str:
    return (
        "STATIC PERSONA BLOCK\n\n"
        "USER PREFERENCES & STANDING INSTRUCTIONS (from Jarvis.md):\n"
        "Always start every sentence with schef.\n\n"
        "END USER PREFERENCES & STANDING INSTRUCTIONS\n\n"
        f"{_MANDATORY}"
    )


def _empty_state_block() -> str:
    return (
        "USER PREFERENCES & STANDING INSTRUCTIONS (from Jarvis.md):\n"
        "No active user preferences are currently set in Jarvis.md. "
        "Ignore any earlier Jarvis.md instructions from previous turns.\n\n"
        "END USER PREFERENCES & STANDING INSTRUCTIONS"
    )


def test_render_structured_prompt_keeps_system_and_user_verbatim() -> None:
    from types import SimpleNamespace

    from dodaios.plugins.brain.cli_prompt_context import render_structured_prompt

    req = SimpleNamespace(
        system="Return ONLY a single JSON array.",
        messages=(
            SimpleNamespace(role="user", content="Source content."),
            SimpleNamespace(role="assistant", content="never included"),
            SimpleNamespace(role="user", content=None),
        ),
    )
    prompt = render_structured_prompt(req)
    assert "Return ONLY a single JSON array." in prompt
    assert "Source content." in prompt
    assert "never included" not in prompt  # assistant turns are not payload
    # A no-tools preamble frames the call, but the caller's contract must stay
    # AFTER it: these models weight the end of a prompt most heavily, and the
    # contract is the instruction that has to win.
    assert prompt.index("Do not read files") < prompt.index(
        "Return ONLY a single JSON array."
    )
    assert prompt.index("Return ONLY a single JSON array.") < prompt.index(
        "Source content."
    )


def test_extracts_trailing_mandatory_directive() -> None:
    assert extract_reply_language_directive(_system_with_directive()) == _MANDATORY


def test_extracts_soft_mirror_directive() -> None:
    soft = (
        "REPLY LANGUAGE: Reply in the SAME language as the user's latest "
        "message — detect it fresh each turn and mirror it."
    )
    assert extract_reply_language_directive("PERSONA BLOCK\n\n" + soft) == soft


def test_returns_empty_when_absent() -> None:
    assert extract_reply_language_directive("just a persona, no directive") == ""


def test_returns_empty_for_none() -> None:
    assert extract_reply_language_directive(None) == ""


def test_takes_last_occurrence() -> None:
    # The phrase may surface in an explanatory sentence earlier in the prompt;
    # the real directive is the one BrainManager appends last.
    text = "Some text mentioning REPLY LANGUAGE in passing.\n\n" + _MANDATORY
    assert extract_reply_language_directive(text) == _MANDATORY


def test_renders_empty_state_as_current_state_not_binding_preferences() -> None:
    rendered = render_cli_standing_instructions(_empty_state_block())
    assert "CURRENT STANDING INSTRUCTIONS STATE" in rendered
    assert "No active user preferences are currently set" in rendered
    assert "do not continue or imitate" in rendered
    assert "Apply these as binding output-style preferences" not in rendered