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.

27.4 KB
Raw Blame
"""What makes a CLI seat's chat session Jarvis rather than a coding agent in a folder.

Three things are handed to the spawned CLI:

* an **MCP config** pointing at this app's own tool server
  (``dodaios.ui.web.mcp_server_routes``), so the model's hands are Jarvis' hands
  — the same catalog the voice path calls, through the same safety gateway.
  The config carries the chat's session id in a header, so the tool server can
  route an approval the executor asks for back to THIS chat's card;
* an **identity** — Jarvis' real system-prompt layers (soul, persona, the
  assistant file, the user's profile, the identity card, core memory, wiki
  context), the chat's own transcript on a fresh conversation, and the surface
  addendum below that says which hands are whose;
* a **system preamble** (the addendum) that tells it who it is and when to
  reach for those tools instead of a shell command.

Why this shape and not "Jarvis' brain drives the turn": the person pays for
these turns through a *subscription* (Claude Code, Codex, …), and a
subscription only pays for the CLI's own agent loop. Driving the loop from
Jarvis' brain would bill per token against an API key instead. So the CLI keeps
the wheel and Jarvis lends it the hands AND the head — the model works INSIDE
the Jarvis harness while the seat stays the person's own, and only the vendor's
official binary ever spends the subscription (maintainer, 2026-08-25).

This surface is deliberately additive: a CLI that cannot mount MCP servers, an
app whose tool gateway is not up yet, or a brain that has not finished building
simply runs with less. Nothing here is allowed to make a turn fail.

The service asks the kit — ``brain_runner and cli_seats`` — rather than a
surface name. Both the Jarvis chat and every Society agent on a subscription
seat reach this module: a Society seat hands in the agent's own briefing as
``prompt_override``, so the CLI answers as that agent, not as Jarvis.
"""

from __future__ import annotations

import json
import logging
import re
import uuid
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Final

from dodaios.core.branding import MCP_TOOLS_SERVER_NAME
from dodaios.core.response_style import CONVERSATIONAL_TURN_REMINDER

log = logging.getLogger(__name__)

#: Where the tool server is mounted on the app's own web server. The trailing
#: slash is load-bearing: a mounted ASGI app owns ``<prefix>/…``, and the app
#: has other routes that match the slash-less form first — a CLI pointed at it
#: gets 405, fails to connect, and the chat silently falls back to being an
#: ordinary coding session (the exact symptom, live 2026-08-24).
_MCP_PATH: Final[str] = "/api/control/mcp/"

#: The server name the CLI shows to the model. Claude Code exposes the tools as
#: ``mcp__<name>__<tool>``; Grok Build as ``<name>__<tool>`` (called through
#: ``use_tool``). The preamble spells both prefixes from this one name, and
#: every reader of a tool name accepts the earlier name as well
#: (``dodaios.core.mcp_names``).
_SERVER_NAME: Final[str] = MCP_TOOLS_SERVER_NAME

#: The request header that carries the chat's session id to the tool server.
#: ``dodaios.mcp.dodaios_tools_server`` reads it (through
#: ``dodaios.ui.web.mcp_server_routes``) and stamps the executor's approval
#: surface with it, so a gate the CLI reaches over MCP is answered by THIS
#: chat's card. Any other client — or a config without it — gets the
#: unattended surface, exactly as before.
HEADER_NAME: Final[str] = "X-Jarvis-Chat-Session"

#: The closing block of a society agent's seat. Its briefing already carries the
#: reply style and the working contract; this adds only the transport facts.
SOCIETY_PREAMBLE: Final[str] = (
    f"Your Dodaios tools (society_* and the rest) come from the `{_SERVER_NAME}` MCP server "
    f"(Claude Code: `mcp__{_SERVER_NAME}__<tool>`; Grok Build: `{_SERVER_NAME}__<tool>` "
    "through use_tool). Your own file and shell tools act on this chat's working folder "
    "only. You are this turn's worker: spawn no sub-agents."
)

_SHARED_PREAMBLE: Final[str] = (
    "You are the person's assistant (your name is stated at the top of this prompt) in "
    "the chat of the Dodaios desktop app; they also talk to you by voice.\n\n"
    + CONVERSATIONAL_TURN_REMINDER
    + "\n\nNo emojis unless the person asks for one.\n\n"
    f"Dodaios tools come from the `{_SERVER_NAME}` MCP server: Claude Code names them "
    f"`mcp__{_SERVER_NAME}__<tool>`; Grok Build names them `{_SERVER_NAME}__<tool>`, called "
    "through use_tool. They act on the running app and the person's connected accounts and "
    "are already signed in; prefer them over a shell, a CLI, a skill or the browser for the "
    "person's own apps, data and machine. Your own file and shell tools cover this chat's "
    f"working folder; `mcp__{_SERVER_NAME}__run_shell` / `{_SERVER_NAME}__run_shell` cover "
    "the rest of the machine.\n\n"
    "When the person selects the browser (@browser, @browser-use, [tools: core:browser]), "
    f"use society_browser from the `{_SERVER_NAME}` MCP server, the live browser in the "
    "Options rail; if it is unavailable, say so.\n\n"
    "Approval cards are pending work, not failures."
)

#: The closing block of the assistant's own chat (the Jarvis surface). There
#: the turn itself is the worker: the background workers of old are retired.
SYSTEM_PREAMBLE: Final[str] = _SHARED_PREAMBLE + " You are this turn's worker: spawn no sub-agents."

#: The closing block of a coding thread in the Agentic IDE (a Claude Code seat
#: with no assistant identity). The same transport facts, but the CLI keeps its
#: own sub-agents: a coding agent splits its work into them, the person asks
#: for them by name, and the thread lists each one under its row. The rule
#: above, applied here, made Claude Code refuse sub-agents it was asked for
#: (2026-11-02).
CODING_PREAMBLE: Final[str] = _SHARED_PREAMBLE


#: The child environment variable the control key travels in — the SAME name
#: the app itself reads it from (``dodaios.core.control_key.ENV_VAR``), so one
#: secret has one name. It never goes on the command line: argv is readable by
#: every process on the machine, and this key is the Control API's whole
#: security boundary.
KEY_ENV_VAR: Final[str] = "JARVIS_CONTROL_API_KEY"

#: How much of the chat's transcript rides along on a fresh conversation.
TRANSCRIPT_MAX_CHARS: Final[int] = 20_000

#: A compact identity for a CLI whose prompt travels on argv (Grok Build):
#: Windows caps a command line at 32 767 characters.
COMPACT_MAX_CHARS: Final[int] = 16_000


def endpoint() -> str | None:
    """This app's own MCP URL, or ``None`` before the web server bound."""
    from dodaios.core import runtime_refs

    base = runtime_refs.get_api_base_url()
    return f"{base.rstrip('/')}{_MCP_PATH}" if base else None


def control_key() -> str | None:
    from dodaios.core import control_key as ck

    return ck.get_control_key()


def mcp_config_json(session_id: str | None = None) -> str | None:
    """The Claude-shaped ``mcpServers`` config, or ``None`` when unavailable.

    ``None`` whenever anything is missing — the web server has not bound yet,
    there is no control key, the gateway is still coming up. The caller then
    spawns the CLI exactly as before instead of handing it a config that would
    fail to connect and cost the turn a confusing error. With ``session_id``
    the config also carries the chat's session header (see ``HEADER_NAME``).
    """
    try:
        url = endpoint()
        if not url or not control_key():
            return None
        headers: dict[str, str] = {
            # Expanded by the CLI from the child env — see KEY_ENV_VAR.
            "Authorization": f"Bearer ${{{KEY_ENV_VAR}}}",
        }
        if session_id:
            headers[HEADER_NAME] = session_id
        return json.dumps(
            {"mcpServers": {_SERVER_NAME: {"type": "http", "url": url, "headers": headers}}},
            ensure_ascii=False,
        )
    except Exception:  # noqa: BLE001 — no Jarvis tools is a degraded chat, never a broken one
        log.warning("agent chat: could not build the Dodaios MCP config", exc_info=True)
        return None


def codex_config_args(session_id: str | None = None) -> list[str]:
    """``-c`` overrides that mount the tool server in ``codex exec``.

    Codex speaks streamable HTTP MCP with a bearer token read from an
    environment variable (``codex mcp add --url --bearer-token-env-var``),
    which is the same shape used here — no config file is written, so a chat
    turn never edits the person's own ``~/.codex/config.toml``. With
    ``session_id`` the chat's session header rides along as an
    ``http_headers`` inline table.
    """
    try:
        url = endpoint()
        if not url or not control_key():
            return []
        args = [
            "-c",
            f'mcp_servers.{_SERVER_NAME}.url="{url}"',
            "-c",
            f'mcp_servers.{_SERVER_NAME}.bearer_token_env_var="{KEY_ENV_VAR}"',
        ]
        if session_id:
            args += [
                "-c",
                f'mcp_servers.{_SERVER_NAME}.http_headers={{"{HEADER_NAME}"="{session_id}"}}',
                "-c",
                f"mcp_servers.{_SERVER_NAME}.required=true",
                # Codex cuts an MCP call off after 60 s by default. An agent's
                # question card waits up to five minutes for the person
                # (agent_chat/questions.py), and a browser task can run long.
                "-c",
                f"mcp_servers.{_SERVER_NAME}.tool_timeout_sec=600",
                # The entrypoint delegates actual actions to Jarvis' executor
                # and approval UI, including on the root chat's browser.
                "-c",
                f'mcp_servers.{_SERVER_NAME}.tools.society_browser.approval_mode="approve"',
            ]
            if session_id.startswith("society:"):
                # Codex exec has no interactive approval channel. Every tool on
                # this server delegates to Jarvis' session catalog and
                # ToolExecutor, which owns the actual policy and chat card.
                # Leaving even one tool at Codex's prompt default causes an
                # unanswerable "approval policy is never" failure.
                args += [
                    "-c",
                    f'mcp_servers.{_SERVER_NAME}.default_tools_approval_mode="approve"',
                ]
        return args
    except Exception:  # noqa: BLE001 — see mcp_config_json
        log.warning("agent chat: could not build the Codex MCP overrides", exc_info=True)
        return []


#: Workspace plugin agy discovers under ``.agents/plugins/``. agy has no
#: ``--mcp-config`` flag; Claude-shaped JSON on argv is ignored.
_AGY_PLUGIN_DIRNAME: Final[str] = "jarvis-hands"


def agy_mcp_server_entry(session_id: str | None = None) -> dict[str, Any] | None:
    """The Antigravity ``mcpServers`` row for this app's tool server.

    agy speaks ``serverUrl`` + ``headers`` (not Claude's ``type``/``url``).
    ``None`` when the app cannot offer the tools — same contract as
    :func:`mcp_config_json`.
    """
    try:
        url = endpoint()
        key = control_key()
        if not url or not key:
            return None
        headers: dict[str, str] = {"Authorization": f"Bearer {key}"}
        if session_id:
            headers[HEADER_NAME] = session_id
        return {"serverUrl": url, "headers": headers}
    except Exception:  # noqa: BLE001 — see mcp_config_json
        log.warning("agent chat: could not build the Dodaios agy MCP entry", exc_info=True)
        return None


def install_agy_jarvis_plugin(cwd: Path, session_id: str | None = None) -> Path | None:
    """Drop a workspace plugin so print-mode agy mounts Jarvis' tools.

    agy discovers MCP from ``.agents/plugins/<name>/mcp_config.json`` in the
    workspace (and from the global ``mcp_config.json``, which this does not
    touch). Additive: a failure here never fails the turn.
    """
    entry = agy_mcp_server_entry(session_id)
    if entry is None:
        return None
    try:
        plugin_dir = Path(cwd) / ".agents" / "plugins" / _AGY_PLUGIN_DIRNAME
        plugin_dir.mkdir(parents=True, exist_ok=True)
        (plugin_dir / "plugin.json").write_text(
            json.dumps({"name": "jarvis-hands"}, ensure_ascii=False),
            encoding="utf-8",
        )
        payload = {"mcpServers": {_SERVER_NAME: entry}}
        (plugin_dir / "mcp_config.json").write_text(
            json.dumps(payload, ensure_ascii=False), encoding="utf-8"
        )
        return plugin_dir
    except Exception:  # noqa: BLE001 — see mcp_config_json
        log.warning("agent chat: could not install the Dodaios agy plugin", exc_info=True)
        return None


#: Grok Build has no ``--mcp-config``. Print-mode discovers HTTP MCP from
#: the project file ``.grok/config.toml`` (cwd, then parents up to the git
#: root). Markers keep our block replaceable without touching other servers.
_GROK_PROJECT_CONFIG: Final[str] = "config.toml"
_GROK_BLOCK_BEGIN: Final[str] = "# BEGIN JARVIS HANDS"
_GROK_BLOCK_END: Final[str] = "# END JARVIS HANDS"


def grok_mcp_server_entry(session_id: str | None = None) -> dict[str, Any] | None:
    """The Grok ``[mcp_servers.<name>]`` row for this app's tool server.

    Grok speaks ``url`` + ``bearer_token_env_var`` + ``headers`` (not Claude's
    ``type``/``url`` JSON). ``None`` when the app cannot offer the tools —
    same contract as :func:`mcp_config_json`. The control key never enters
    the file: it travels in the child env under :data:`KEY_ENV_VAR`.
    """
    try:
        url = endpoint()
        if not url or not control_key():
            return None
        entry: dict[str, Any] = {
            "url": url,
            "bearer_token_env_var": KEY_ENV_VAR,
            "enabled": True,
        }
        if session_id:
            entry["headers"] = {HEADER_NAME: session_id}
        return entry
    except Exception:  # noqa: BLE001 — see mcp_config_json
        log.warning("agent chat: could not build the Dodaios grok MCP entry", exc_info=True)
        return None


def _toml_string(value: str) -> str:
    return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"'


def _grok_jarvis_block(entry: dict[str, Any]) -> str:
    lines = [
        _GROK_BLOCK_BEGIN,
        "# Written by Dodaios so Grok Build can reach Dodaios' tools.",
        f"[mcp_servers.{_SERVER_NAME}]",
        f"url = {_toml_string(str(entry['url']))}",
        f"bearer_token_env_var = {_toml_string(str(entry['bearer_token_env_var']))}",
        "enabled = true",
    ]
    headers = entry.get("headers")
    if isinstance(headers, dict) and headers:
        lines.append("")
        lines.append(f"[mcp_servers.{_SERVER_NAME}.headers]")
        for key, value in headers.items():
            lines.append(f"{_toml_string(str(key))} = {_toml_string(str(value))}")
    lines.append(_GROK_BLOCK_END)
    return "\n".join(lines) + "\n"


def _merge_grok_jarvis_block(existing: str, block: str) -> str:
    """Replace a previous Jarvis block, else append. Never drop other servers."""
    if _GROK_BLOCK_BEGIN in existing and _GROK_BLOCK_END in existing:
        pattern = re.compile(
            re.escape(_GROK_BLOCK_BEGIN) + r".*?" + re.escape(_GROK_BLOCK_END) + r"\n?",
            re.DOTALL,
        )
        return pattern.sub(block, existing, count=1)
    body = existing.rstrip()
    return block if not body else body + "\n\n" + block


def install_grok_jarvis_mcp(cwd: Path, session_id: str | None = None) -> Path | None:
    """Write a project MCP row so print-mode grok mounts Jarvis' tools.

    grok discovers HTTP MCP from ``.grok/config.toml`` in the working folder
    (no ``--mcp-config`` flag; Claude-shaped JSON on argv is ignored). The
    control key stays in the child env. Additive: a failure here never fails
    the turn.
    """
    entry = grok_mcp_server_entry(session_id)
    if entry is None:
        return None
    try:
        config_path = Path(cwd) / ".grok" / _GROK_PROJECT_CONFIG
        config_path.parent.mkdir(parents=True, exist_ok=True)
        previous = ""
        if config_path.is_file():
            previous = config_path.read_text(encoding="utf-8")
        config_path.write_text(
            _merge_grok_jarvis_block(previous, _grok_jarvis_block(entry)),
            encoding="utf-8",
        )
        return config_path
    except Exception:  # noqa: BLE001 — see mcp_config_json
        log.warning("agent chat: could not install the Dodaios grok MCP config", exc_info=True)
        return None


def apply_env(env: dict[str, str]) -> dict[str, str]:
    """Put the control key into a child environment, if there is one to give."""
    key = control_key()
    if key:
        env[KEY_ENV_VAR] = key
    return env


def tool_count() -> int:
    """How many Jarvis tools the chat would currently be offered (0 = none)."""
    try:
        from dodaios.mcp.dodaios_tools_server import offered_tools

        return len(offered_tools())
    except Exception:  # noqa: BLE001 — a count is a nicety, never a failure
        return 0


# ------------------------------------------------------------------ identity


@dataclass(slots=True)
class Identity:
    """What a CLI seat is told it is, this turn.

    ``text`` is the full identity (Jarvis' layers, the transcript, the
    addendum); ``compact`` a shorter cut for a CLI whose prompt travels on
    argv; ``path`` the file the full text was written to for a CLI that takes
    a system-prompt file (Claude Code), removed by the runner afterwards.
    """

    session_id: str
    text: str
    compact: str
    path: Path | None = None


def _brain() -> Any | None:
    from dodaios.core import runtime_refs

    return runtime_refs.get_brain_manager()


async def identity_prompt(
    *,
    user_text: str,
    history: list[dict[str, Any]],
    resume: str | None,
    prompt_override: str | None = None,
) -> str:
    """Jarvis' real prompt layers + the chat's transcript + the addendum.

    The layers come from the live brain (``BrainManager.render_surface_prompt``:
    soul, persona, the assistant file, the user's profile, contacts, the
    identity card, core memory, the wiki context for ``user_text``, and the
    turn context). Without a brain — before it finished building, or on a
    box that runs the chat alone — the addendum stands in, as it did before:
    nothing here may fail a turn.

    The transcript rides along on a FRESH conversation only: a CLI that
    resumes its own session already carries it, and repeating it there would
    tell the model the same story twice.
    """
    parts: list[str] = []
    if prompt_override:
        # A society agent's seat: ITS briefing is the identity. Jarvis' own
        # layers stay out so the CLI does not answer as Jarvis.
        parts.append(prompt_override.strip())
        if resume is None and history:
            transcript = render_transcript(history)
            if transcript:
                parts.append("## This conversation so far\n\n" + transcript)
        parts.append(SOCIETY_PREAMBLE)
        return "\n\n".join(parts)
    brain = _brain()
    render = getattr(brain, "render_surface_prompt", None) if brain is not None else None
    if callable(render):
        try:
            system_prompt, turn_context = await render(user_text=user_text)
            if system_prompt.strip():
                parts.append(system_prompt.strip())
            if turn_context.strip():
                parts.append(turn_context.strip())
        except Exception:  # noqa: BLE001 — the addendum alone is still a working chat
            log.warning("agent chat: Dodaios' prompt layers unavailable this turn", exc_info=True)
    if resume is None and history:
        transcript = render_transcript(history)
        if transcript:
            parts.append("## This conversation so far\n\n" + transcript)
    parts.append(SYSTEM_PREAMBLE)
    return "\n\n".join(parts)


def render_transcript(
    history: list[dict[str, Any]], *, max_chars: int = TRANSCRIPT_MAX_CHARS
) -> str:
    """The chat's log as plain prose, newest last, cut to ``max_chars`` from the front.

    The model reading this IS the second speaker, whatever it is called (the
    assistant on the app's own chat, a named agent on its seat), so its own
    lines are labelled ``You``. Nothing parses these labels.

    A turn's tool calls appear as the same labelled tool record the in-process
    brain replays (``runner_brain.brain_history_from_events``): each call with
    its bounded result, worded as data. A bare "[used tool X]" line without the
    result taught smaller models to type that line instead of calling the tool
    (issue #978).
    """
    from dodaios.agent_chat.runner_brain import brain_history_from_events

    lines: list[str] = []
    for message in brain_history_from_events(history, max_messages=None):
        text = message.content.strip() if isinstance(message.content, str) else ""
        if not text:
            continue
        if message.role == "user":
            lines.append(f"Person: {text}")
        elif message.role == "assistant":
            lines.append(f"You: {text}")
    out = "\n\n".join(lines)
    if len(out) > max_chars:
        out = "…" + out[-max_chars:]
    return out


def _preamble_of(text: str) -> str:
    """The closing preamble an identity ends with (a society seat's or the assistant's)."""
    return SOCIETY_PREAMBLE if text.endswith(SOCIETY_PREAMBLE) else SYSTEM_PREAMBLE


def compact_identity(text: str, *, max_chars: int = COMPACT_MAX_CHARS) -> str:
    """The identity cut for argv: the addendum is kept whole, the rest trimmed."""
    if len(text) <= max_chars:
        return text
    preamble = _preamble_of(text)
    head_room = max(0, max_chars - len(preamble) - 8)
    body = text[: -len(preamble)] if text.endswith(preamble) else text
    body = body[:head_room].rstrip()
    body = re.sub(r"\n[^\n]*$", "", body)  # do not cut a line in half
    return f"{body}\n\n…\n\n{preamble}"


#: A created agent's chat never ends (MASTERPLAN §2.10), but a vendor CLI's own
#: conversation cannot grow forever. Once the chat has grown this much since the
#: CLI conversation began (roughly 120k tokens), the next turn starts a fresh
#: CLI conversation from the agent's briefing, memory and recent transcript.
SOCIETY_ROLLOVER_CHARS: Final[int] = 480_000
#: The notice kind that marks where a fresh CLI conversation began.
ROLLOVER_NOTICE_KIND: Final[str] = "context_rollover"


def society_rollover_due(history: list[dict[str, Any]]) -> bool:
    """Whether the chat outgrew its CLI conversation since the last rollover."""
    start = 0
    for index, event in enumerate(history):
        payload = event.get("payload") or {}
        if event.get("kind") == "notice" and payload.get("kind") == ROLLOVER_NOTICE_KIND:
            start = index + 1
    size = 0
    for event in history[start:]:
        size += len(json.dumps(event.get("payload") or {}, ensure_ascii=False))
        if size > SOCIETY_ROLLOVER_CHARS:
            return True
    return False


def society_memory_refresh(text: str, *, compact: bool = False) -> str:
    """Refresh mutable guidance on resumed CLI turns without replaying the transcript."""
    headings = (
        "Choosing your name",
        "You are new",
        "Standing instructions",
        "Your memory",
        "Your user profile",
        "Learned working instructions",
        "Your learned skills (run one with society_run_skill)",
    )
    sections = []
    for section in re.split(r"(?m)(?=^## )", text.removesuffix(_preamble_of(text))):
        title = section.split("\n", 1)[0].removeprefix("## ").strip()
        if title in headings or title.startswith("You are "):
            sections.append(section.strip())
    body = "\n\n".join(sections)
    note = "An absent section has no current entries. "
    if compact and len(body) > COMPACT_MAX_CHARS - 1000:
        # argv transports keep their existing Windows command-line budget.
        # Never present a partial snapshot as deletion of the omitted rules.
        body = body[: COMPACT_MAX_CHARS - 1000].rsplit("\n", 1)[0]
        note = (
            "This transport carries an excerpt, not a complete replacement. Omitted entries "
            "are not deletions; recall current facts with society_memory_recall as needed. "
        )
    return (
        "<current_agent_memory>\n"
        "These sections replace their earlier snapshots. "
        + note
        + "Without a naming section keep your current name (earlier placeholder instructions "
        "no longer apply). Learned guidance never grants permissions.\n\n"
        + body
        + "\n</current_agent_memory>\n\n"
    )


#: Lines of a society briefing that state this turn's live facts (surface.py):
#: the client timezone, the capability epoch, the stored credentials and the
#: unattended-routine marker.
_CREDENTIAL_STATE = re.compile(
    r"^(?:Client timezone for this turn:|Capability epoch:|Stored credentials\b"
    r"|This is an unattended routine run\b).*$",
    re.M,
)


def society_turn_state(text: str) -> str:
    """The briefing's live state lines, sent on every resumed CLI turn.

    A resumed vendor conversation never sees the briefing again, so without
    this it keeps believing whatever the chat once said about a token.
    """
    return "\n".join(m.group(0).strip() for m in _CREDENTIAL_STATE.finditer(text or ""))


#: Earlier name, kept for callers that read only the credential lines.
society_credential_state = society_turn_state


def identity_dir() -> Path:
    """Where identity files live: the app's own data dir, never a shared temp."""
    from dodaios.core.paths import user_data_dir

    return user_data_dir() / "agent_chat" / "identity"


def write_identity_file(text: str, *, turn_id: str) -> Path:
    """Write the identity for a CLI that takes a system-prompt FILE.

    Claude Code's ``--append-system-prompt-file`` exists because argv cannot
    carry a prompt this size on Windows (32 767 characters for the whole
    command line; Jarvis' layers alone run past 50 000). The runner removes
    the file when the turn ends.
    """
    folder = identity_dir()
    folder.mkdir(parents=True, exist_ok=True)
    path = folder / f"{turn_id or uuid.uuid4().hex}.md"
    path.write_text(text, encoding="utf-8")
    return path


def remove_identity_file(path: Path | None) -> None:
    if path is None:
        return
    try:
        path.unlink(missing_ok=True)
    except OSError:
        log.debug("agent chat: identity file %s not removed", path, exc_info=True)


async def build_identity(
    *,
    session_id: str,
    turn_id: str,
    user_text: str,
    history: list[dict[str, Any]],
    resume: str | None,
    with_file: bool,
    prompt_override: str | None = None,
) -> Identity:
    """The identity for one CLI turn; ``with_file`` for a CLI that takes a file."""
    text = await identity_prompt(
        user_text=user_text, history=history, resume=resume, prompt_override=prompt_override
    )
    path = write_identity_file(text, turn_id=turn_id) if with_file else None
    return Identity(session_id=session_id, text=text, compact=compact_identity(text), path=path)


__all__ = [
    "COMPACT_MAX_CHARS",
    "HEADER_NAME",
    "KEY_ENV_VAR",
    "CODING_PREAMBLE",
    "SOCIETY_PREAMBLE",
    "SYSTEM_PREAMBLE",
    "TRANSCRIPT_MAX_CHARS",
    "Identity",
    "agy_mcp_server_entry",
    "apply_env",
    "build_identity",
    "codex_config_args",
    "compact_identity",
    "control_key",
    "endpoint",
    "identity_dir",
    "identity_prompt",
    "grok_mcp_server_entry",
    "install_agy_jarvis_plugin",
    "install_grok_jarvis_mcp",
    "mcp_config_json",
    "remove_identity_file",
    "render_transcript",
    "tool_count",
    "write_identity_file",
]