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.

36.2 KB
Raw Blame
"""Explicit chat commands and one durable goal supervisor per session."""

from __future__ import annotations

import asyncio
import hashlib
import json
import logging
import random
import re
import time
import uuid
from contextvars import ContextVar
from typing import Any

from .control_store import ControlStore
from .control_types import (
    COMMANDS,
    ChatControlState,
    CommandRequest,
    CommandResult,
    GoalState,
    GoalVerdict,
)
from .parked_work import BACKGROUND_ORIGIN

log = logging.getLogger(__name__)
_command_origin: ContextVar[CommandRequest | None] = ContextVar("chat_command_origin", default=None)


def supports_restricted_turn(session: Any) -> bool:
    from .service import session_runner

    return session_runner(session) not in ("kimi-cli", "dsh-cli")


def _chat_language(current: str, text: str) -> str:
    """The conversation language after *text*, sticky, else *current*.

    Never the UI language: the interface being English says nothing about
    the language a person chats in (forensic 2026-10-19, a Turkish chat in an
    English UI was forced into English after a slash command).
    """
    from dodaios.core import runtime_refs
    from dodaios.core.turn_language import resolve_conversation_language
    from dodaios.i18n import is_known_language

    brain = runtime_refs.get_brain_manager()
    language = resolve_conversation_language(
        getattr(brain, "reply_language", "auto") or "auto",
        "unknown",
        text or "",
        conversation_language=current,
    )
    return language if is_known_language(language) else current


class ChatControls:
    def __init__(self, service: Any, *, evaluator: Any = None, adapters: Any = None) -> None:
        self.service = service
        self.store = ControlStore(service.store)
        self.evaluator = evaluator
        self.adapters = adapters
        self.jobs: dict[str, asyncio.Task] = {}
        self.native: dict[str, Any] = {}
        self.locks: dict[str, asyncio.Lock] = {}
        self._closed = False

    def state(self, sid: str) -> ChatControlState:
        session = self.service.store.get_session(sid)
        if session is None:
            raise ValueError("Session not found")
        state = self.store.get(sid)
        if state.revision == 0 and not state.last_request:
            events = self.service.store.list_events(sid)
            messages = [e for e in events if e["kind"] == "user_message"]
            if messages:
                latest = messages[-1]
                state.last_request = str(
                    latest["payload"].get("typed") or latest["payload"].get("text") or ""
                )
                finished = [
                    e for e in events if e["kind"] == "turn_finished" and e["seq"] > latest["seq"]
                ]
                if self.service.is_running(sid):
                    state.last_status = "running"
                elif finished:
                    state.last_status = (
                        "done" if finished[-1]["payload"].get("status") == "done" else "interrupted"
                    )
                else:
                    state.last_status = "interrupted"
        state.permission_mode = session.permission_mode
        state.mode = "plan" if session.permission_mode in ("plan", "read-only") else "build"
        return state

    def catalog(self, sid: str | None = None) -> dict[str, Any]:
        state = self.state(sid) if sid else None
        restricted = not sid or supports_restricted_turn(self.service.store.get_session(sid))
        return {
            "commands": [
                {
                    **row,
                    "available": restricted
                    or row["name"] not in ("plan", "review", "recap", "goal"),
                    "reason": ""
                    if restricted
                    else "This runner cannot enforce read-only verification; choose an API model",
                }
                for row in COMMANDS
            ],
            "state": state.model_dump() if state else None,
        }

    async def publish(self, state: ChatControlState) -> None:
        self.store.save(state)
        await self.service.post_notice(
            state.session_id,
            {
                "kind": "chat_control",
                "text": "",
                "state": state.model_dump(),
            },
        )

    async def execute(self, sid: str, request: CommandRequest) -> CommandResult:
        session = self.service.store.get_session(sid)
        if session is None or session.surface not in ("jarvis", "society"):
            raise ValueError("Commands are available in Dodaios and agent chats only")
        # Stop must remain usable while another command waits for a turn or an approval.
        if request.command == "stop":
            return await self._execute(sid, request)
        async with self.locks.setdefault(sid, asyncio.Lock()):
            return await self._execute(sid, request)

    async def _execute(self, sid: str, request: CommandRequest) -> CommandResult:
        previous = self.store.claim(sid, request)
        if previous is not None:
            return previous
        result = CommandResult(
            request_id=request.request_id, command=request.command, state=self.state(sid)
        )
        origin_token = _command_origin.set(request)
        try:
            session = self.service.store.get_session(sid)
            binder = getattr(self.service, "bind_society_session", None)
            if (
                request.command
                not in {"stop", "status", "help", "clear", "history", "model", "routines", "find"}
                and session is not None
                and session.surface == "society"
                and binder is not None
            ):
                await binder(sid)
            from dodaios.memory.wiki.secret_guard import contains_secret

            if contains_secret(request.arguments):
                raise ValueError("Configure credentials in app settings, not in chat commands")
            state = self.state(sid)
            state.output_language = _chat_language(
                state.output_language, request.arguments or state.last_request
            )
            self.store.save(state)
            data = await self._dispatch(
                sid, request.command, request.arguments.strip(), request.attachments
            )
            result.data = data
            result.status = "started" if data.get("turn_id") or data.get("started") else "done"
        except Exception as exc:
            log.warning("Chat command %s failed in %s: %s", request.command, sid, exc)
            result.status, result.error = "failed", str(exc)
        finally:
            _command_origin.reset(origin_token)
        result.state = self.state(sid)
        self.store.finish(sid, result)
        return result

    async def _dispatch(
        self, sid: str, command: str, arguments: str, attachments: list[dict] | None = None
    ) -> dict[str, Any]:
        attachments = attachments or []
        state = self.state(sid)
        session = self.service.store.get_session(sid)
        if command in ("plan", "review", "recap", "goal") and not supports_restricted_turn(session):
            if command != "goal" or arguments not in ("", "clear"):
                raise ValueError(
                    "This runner cannot enforce read-only verification; choose an API model"
                )
        if command in ("help", "clear", "history", "model", "routines"):
            return {"local_action": command}
        if command == "status" or command == "goal" and not arguments:
            return {"running": self.service.is_running(sid)}
        if command == "stop":
            await self.pause(sid, "Stopped by the user")
            return {"stopped": True}
        if command == "goal":
            if arguments == "clear":
                if state.goal and state.goal.status == "active":
                    await self.pause(sid, "Goal cleared by the user")
                await self._clear_saved_native(sid)
                state = self.state(sid)
                if state.goal:
                    state.goal.status = "cleared"
                    state.last_status = "idle"
                    self.store.save_inputs(sid, [])
                    await self.publish(state)
                return {}
            if state.mode == "plan":
                raise ValueError("Leave plan mode with /build before starting a goal")
            if len(arguments) > 4000:
                raise ValueError("Goal must be at most 4000 characters")
            await self.pause(sid, "Replaced by a new goal")
            state = self.state(sid)
            now = int(time.time() * 1000)
            state.goal = GoalState(
                id=uuid.uuid4().hex, objective=arguments, started_ms=now, updated_ms=now
            )
            state.last_request = arguments
            state.last_status = "running"
            self.store.save_inputs(sid, attachments)
            await self.publish(state)
            from .events import make_event

            await self.service._emit(
                sid, make_event("user_message", {"text": "/goal " + arguments})
            )
            self.start(sid)
            return {"started": True}
        if command == "plan":
            await self.pause(sid, "Plan mode activated")
            state = self.state(sid)
            if state.mode != "plan":
                state.previous_permission = session.permission_mode
            await self._mode(sid, "plan", state)
            if arguments or attachments:
                arguments = (
                    arguments
                    or "Read the attached materials and prepare a plan without making changes."
                )
                state.last_request = arguments
                await self.publish(state)
                return {
                    "turn_id": await self._send(
                        sid, arguments, attachments=attachments, control_owned=True
                    )
                }
            return {}
        if command == "build":
            self._idle(sid)
            permission = state.previous_permission or session.permission_mode
            if permission in ("plan", "read-only"):
                raise ValueError(
                    "No writable mode was selected before planning; "
                    "update the agent permissions first"
                )
            await self._mode(sid, permission, state)
            if state.plan:
                return {
                    "turn_id": await self._send(
                        sid,
                        "Implement the agreed plan. Preserve its constraints.\n\n" + state.plan,
                        control_owned=True,
                    )
                }
            return {}
        if command == "continue":
            self._idle(sid)
            if state.mode == "plan" and state.goal:
                raise ValueError("Leave plan mode with /build before resuming a goal")
            if state.goal and state.goal.status in ("paused", "blocked"):
                state.goal.status = "active"
                state.last_status = "running"
                state.goal.reason = ""
                state.goal.stalled_steps = 0
                await self.publish(state)
                self.start(sid)
                return {"started": True}
            if state.goal and state.goal.status == "active":
                return {"started": sid in self.jobs}
            if not state.last_request or state.last_status not in ("interrupted", "failed"):
                if self._last_failure_was_not_yours(sid):
                    raise ValueError(
                        "The task that failed last came from Dodaios, a teammate or a "
                        "routine, not from you, so /continue cannot resume it. Ask the "
                        "agent yourself, or let the sender try again."
                    )
                raise ValueError("There is no interrupted task to continue")
            return {
                "turn_id": await self._send(
                    sid,
                    "Continue the interrupted task from its existing progress; "
                    "do not repeat completed actions.\n\n" + state.last_request,
                    control_owned=True,
                )
            }
        if command == "find":
            if not arguments:
                raise ValueError("Use /find followed by search text")
            hits = []
            for event in self.service.store.list_events(sid):
                payload = event.get("payload") or {}
                if payload.get("origin") in ("control", BACKGROUND_ORIGIN):
                    continue  # nothing the person typed
                if event["kind"] not in ("user_message", "assistant_text", "agent_message"):
                    continue
                text = str(payload.get("typed") or payload.get("text") or "")
                if arguments.casefold() in text.casefold():
                    hits.append(
                        {
                            "seq": event["seq"],
                            "kind": event["kind"],
                            "text": text,
                            "turn_id": payload.get("turn_id"),
                            "session_id": sid,
                            "item_id": f"u-{event['seq']}"
                            if event["kind"] == "user_message"
                            else payload.get("turn_id") or payload.get("message_id"),
                        }
                    )
            return {"hits": hits}
        if command in ("remember", "message"):
            if attachments:
                raise ValueError("This command accepts text; send files as a separate chat message")
            self._idle(sid)
            if state.mode == "plan":
                raise ValueError("Plan mode permits reads only; leave it with /build")
            if not arguments:
                raise ValueError(f"/{command} needs an argument")
            return await self._tool(sid, command, arguments)
        if command in ("recap", "review"):
            self._idle(sid)
            prompt = (
                "Summarize this conversation briefly: current state, the most important decision, "
                "and exactly one next action. Do not carry out any actions."
                if command == "recap"
                else "Review the latest work product against the user's actual request. "
                "Inspect available evidence, distinguish confirmed results from claims, "
                "and list concrete defects or missing acceptance criteria. "
                "Do not modify anything or send messages."
            )
            return {
                "turn_id": await self._send(
                    sid,
                    prompt,
                    read_only=True,
                    control_owned=True,
                    direct_user=False,
                    attachments=attachments,
                )
            }
        raise ValueError("Unknown command")

    def _idle(self, sid: str) -> None:
        if self.service.is_running(sid) or sid in self.jobs:
            raise ValueError("The chat is working; use /stop first")

    async def _send(self, sid: str, text: str, **kwargs: Any) -> str:
        if kwargs.get("direct_user", True):
            state = self.state(sid)
            state.last_request, state.last_status = text, "running"
            # Keep the stored language the conversation's, turn by turn, so a
            # later send never inherits one an older command left behind.
            state.output_language = _chat_language(state.output_language, text)
            self.store.save(state)
            origin = _command_origin.get()
            if origin:
                kwargs["display_text"] = f"/{origin.command} {origin.arguments}".rstrip()
        return await self.service.send(
            sid, text, output_language=self.state(sid).output_language, **kwargs
        )

    async def _mode(self, sid: str, permission: str, state: ChatControlState) -> None:
        from dodaios.core.tool_read_only import set_chat_read_only

        from .events import make_event
        from .permissions import ladder_key, normalize_permission, society_mode_supported
        from .service import session_runner

        session = self.service.store.get_session(sid)
        runner = session_runner(session)
        ladder = ladder_key(session.surface, runner)
        # /plan is a chat control, not a ladder choice: the Society ladder
        # (bypass / ask / always ask) has no read-only rung, and folding
        # "plan" onto it would silently leave the turn able to write.
        folded = normalize_permission(ladder, permission)
        if permission in ("plan", "read-only") and folded not in ("plan", "read-only"):
            folded = "plan"
        permission = folded
        if session.surface == "society" and self.service.is_running(sid):
            raise ValueError("The agent chat is working; stop it before changing permissions")
        if session.surface == "society" and permission not in ("plan", "read-only"):
            if not society_mode_supported(runner, permission):
                raise ValueError(f"{runner} cannot provide an actionable approval for {permission}")
        self.service.store.update_session(sid, permission_mode=permission)
        if session.surface == "society":
            self.service.store.set_permission_override(sid, permission)
            binder = getattr(self.service, "bind_society_session", None)
            bound = await binder(sid) if binder is not None else session
            self.service.store.set_permission_override(sid, bound.permission_mode)
            if bound.permission_mode != permission:
                raise ValueError("The agent's roster does not permit that chat mode")
            permission = bound.permission_mode
        state.mode = "plan" if permission in ("plan", "read-only") else "build"
        set_chat_read_only(sid, state.mode == "plan")
        await self.publish(state)
        await self.service._emit(
            sid, make_event("session_updated", {"permission_mode": permission})
        )

    async def _tool(self, sid: str, command: str, arguments: str) -> dict[str, Any]:
        from dodaios.society.agent_tools import MessageAgentTool, WikiNoteTool
        from dodaios.society.runtime import current_runtime

        from .runner_brain import brain_manager

        session = self.service.store.get_session(sid)
        runtime = current_runtime()
        brain = brain_manager()
        executor = getattr(brain, "_tool_executor", None)
        if runtime is None or executor is None:
            raise ValueError("Agent tools are still starting")
        from dodaios.society.surface import agent_id_of

        agent_id = (agent_id_of(sid) or "") if session.surface == "society" else "jarvis"
        tool: Any
        if command == "message":
            match = re.fullmatch(r'@(?P<target>"[^"]+"|\S+)\s+(?P<text>[\s\S]+)', arguments)
            if not match:
                raise ValueError("Use /message @Agent text (quote names containing spaces)")
            tool = MessageAgentTool(runtime, agent_id)
            args = {"target": match["target"].strip('"'), "text": match["text"], "kind": "say"}
        else:
            tool = WikiNoteTool(runtime, agent_id)
            args = {"text": arguments, "kind": "memory", "origin": "user"}
        from .approval_bridge import ChatGrant, approval_ref
        from .events import make_event

        completed: list[Any] = []

        async def run(handle: Any, text: str) -> None:
            bridge = self.service._bridge_for(self.service._bus())
            ref = approval_ref(sid)
            if bridge is not None:
                bridge.arm(
                    ref,
                    ChatGrant(
                        session_id=sid,
                        turn_id=handle.turn_id,
                        stance=session.permission_mode,
                        always_allowed=self.service.always_allowed(sid),
                        ask=handle.request_approval,
                    ),
                )
            await handle.emit(
                make_event(
                    "tool_call",
                    {
                        "turn_id": handle.turn_id,
                        "call_id": "command",
                        "name": tool.name,
                        "input": args,
                        "summary": f"/{command}",
                    },
                )
            )
            try:
                outcome = await executor.execute(
                    tool,
                    args,
                    trace_id=handle.trace_id,
                    user_utterance=text,
                    config_snapshot={
                        "approval_surface": "interactive",
                        "approval_ref": ref,
                        "tool_origin": "society",
                        "cwd": session.cwd,
                    },
                )
                completed.append(outcome)
                await handle.emit(
                    make_event(
                        "tool_result",
                        {
                            "turn_id": handle.turn_id,
                            "call_id": "command",
                            "output": json.dumps(outcome.output, ensure_ascii=False),
                            "is_error": not outcome.success,
                            "duration_ms": 0,
                        },
                    )
                )
                await handle.emit(
                    make_event(
                        "turn_finished",
                        {
                            "turn_id": handle.turn_id,
                            "status": "done" if outcome.success else "error",
                            "error": outcome.error,
                            "duration_ms": 0,
                            "usage": {},
                        },
                    )
                )
            finally:
                if bridge is not None:
                    bridge.disarm(ref)

        await self._send(sid, f"/{command} {arguments}", control_owned=True, control_runner=run)
        await self.service.wait_turn(sid)
        if not completed or not completed[-1].success:
            raise ValueError(completed[-1].error if completed else "Command was interrupted")
        return {"result": completed[-1].output}

    async def user_message(self, sid: str, text: str) -> int | None:
        state = self.state(sid)
        if state.goal and state.goal.native_pending and state.goal.status != "active":
            await self._clear_saved_native(sid)
            state = self.state(sid)
        active = state.goal is not None and state.goal.status == "active"
        if self.service.is_running(sid) and not active:
            return None
        if active:
            await self.pause(sid, "User is steering the task")
            state = self.state(sid)
            if state.goal is not None:
                state.goal.status = "active"
        state.last_request = text
        state.last_status = "running"
        self.store.save(state)
        return state.revision

    async def message_rejected(self, sid: str, text: str, revision: int) -> None:
        """Close a control status when validation rejects a message before its turn.

        A later control write owns a newer revision and must not be rolled back.
        Keep an active goal for a later explicit continuation, but pause it
        because no runner was admitted for this message.
        """
        state = self.state(sid)
        if (
            state.revision != revision
            or state.last_request != text
            or state.last_status != "running"
            or self.service.is_running(sid)
        ):
            return
        if state.goal and state.goal.status == "active":
            state.goal.status = "paused"
            state.goal.reason = "Message was not started"
            state.goal.updated_ms = int(time.time() * 1000)
            state.last_status = "interrupted"
        else:
            state.last_status = "failed"
        await self.publish(state)

    def _last_failure_was_not_yours(self, sid: str) -> bool:
        """Whether the chat's latest turn failed and the person did not start it.

        The service emits a person's ``user_message`` right before its
        ``turn_started``; work delivered by Jarvis, a teammate or a routine has
        none, and ``/continue`` only resumes the person's own request
        (``turn_completed``).
        """
        events = self.service.store.list_events(sid)
        finished = [e for e in events if e["kind"] == "turn_finished"]
        if not finished or finished[-1]["payload"].get("status") not in ("error", "cancelled"):
            return False
        turn_id = finished[-1]["payload"].get("turn_id")
        for index, event in enumerate(events):
            if event["kind"] == "turn_started" and event["payload"].get("turn_id") == turn_id:
                before = [e for e in events[:index] if e["kind"] != "notice"]
                return not before or before[-1]["kind"] != "user_message"
        return False

    async def turn_completed(
        self, sid: str, turn_id: str, text: str, direct_user: bool, read_only: bool
    ) -> None:
        state = self.state(sid)
        terminal = [
            e
            for e in self.service.store.list_events(sid)
            if e["kind"] == "turn_finished" and e["payload"].get("turn_id") == turn_id
        ]
        status = terminal[-1]["payload"].get("status") if terminal else "error"
        if direct_user:
            state.last_status = (
                "done" if status == "done" else "interrupted" if status == "cancelled" else "failed"
            )
            self.store.save(state)
        if state.mode == "plan" and not read_only and status == "done":
            answers = [
                str(e["payload"].get("text") or "")
                for e in self.service.store.list_events(sid)
                if e["kind"] == "assistant_text" and e["payload"].get("turn_id") == turn_id
            ]
            if answers:
                state.plan = "\n".join(answers)
                await self.publish(state)
        if direct_user and state.goal and state.goal.status == "active" and sid not in self.jobs:
            self.start(sid)

    def start(self, sid: str) -> None:
        if sid in self.jobs or self._closed:
            return
        self.jobs[sid] = asyncio.create_task(self._goal_loop(sid), name=f"chat-goal-{sid}")

    async def pause(self, sid: str, reason: str) -> None:
        state = self.state(sid)
        if self.service.is_running(sid):
            state.last_status = "interrupted"
            self.store.save(state)
        if state.goal and state.goal.status == "active":
            state.goal.status, state.goal.reason = "paused", reason
            state.last_status = "interrupted"
            await self.publish(state)
        native = self.native.get(sid)
        task = self.jobs.pop(sid, None)
        if task and task is not asyncio.current_task():
            task.cancel()
            await asyncio.gather(task, return_exceptions=True)
        try:
            if native:
                await native.interrupt(sid)
        finally:
            await self.service.cancel(sid)
        if native and getattr(native, "cleanup_done", False):
            state = self.state(sid)
            if state.goal:
                state.goal.native_pending = False
                await self.publish(state)

    async def _clear_saved_native(self, sid: str) -> None:
        state = self.state(sid)
        if not state.goal or not state.goal.native_pending:
            return
        from .native_control import native_adapters

        adapter = next((item for item in native_adapters() if item.name == state.goal.engine), None)
        if adapter is None:
            raise ValueError(
                "The previous native goal must be cleared before this chat can continue"
            )
        await adapter.clear_saved(self.service, self.service.store.get_session(sid))
        state.goal.native_pending = False
        await self.publish(state)

    async def _goal_loop(self, sid: str) -> None:
        initial = self.state(sid)
        if initial.goal is None:
            return
        goal_id = initial.goal.id
        previous_digest = ""
        background_update = ""
        try:
            from .control_eval import evaluate_goal
            from .native_control import native_adapters

            session = self.service.store.get_session(sid)
            if initial.goal.native_pending:
                await self._clear_saved_native(sid)
            attachments = self.store.inputs(sid)
            if attachments:
                await self._send(
                    sid,
                    "Read these attachments as context for the upcoming goal. "
                    "Do not take actions yet.",
                    attachments=attachments,
                    read_only=True,
                    direct_user=False,
                    control_owned=True,
                )
                await self.service.wait_turn(sid)
                finished = [
                    e for e in self.service.store.list_events(sid) if e["kind"] == "turn_finished"
                ]
                if not finished or finished[-1]["payload"].get("status") != "done":
                    raise ValueError("Attachments could not be read; they were kept for /continue")
                self.store.save_inputs(sid, [])
            adapters = self.adapters if self.adapters is not None else native_adapters()
            for adapter in adapters:
                if await adapter.available(session):
                    state = self.state(sid)
                    if state.goal is None:
                        return
                    state.goal.engine = adapter.name
                    state.goal.native_pending = True
                    await self.publish(state)
                    self.native[sid] = adapter
                    outcome = await adapter.run_goal(self.service, session, state.goal.objective)
                    state = self.state(sid)
                    if state.goal and state.goal.id == goal_id and state.goal.status == "active":
                        state.goal.status = outcome["status"]
                        state.goal.native_pending = not getattr(adapter, "cleanup_done", False)
                        state.goal.steps += 1
                        state.goal.updated_ms = int(time.time() * 1000)
                        state.last_status = "done" if state.goal.status == "complete" else "failed"
                        state.goal.reason = outcome.get("reason", "")
                        state.goal.evidence = outcome.get("evidence", [])
                        await self.publish(state)
                    return
            while not self._closed:
                state = self.state(sid)
                if not state.goal or state.goal.id != goal_id or state.goal.status != "active":
                    return
                if self.service.is_running(sid):
                    await self.service.wait_turn(sid)
                    continue
                events = self.service.store.list_events(sid)
                after = events[-1]["seq"] if events else 0
                task = (
                    "Work toward this goal and show verifiable evidence of progress:\n"
                    + state.goal.objective
                )
                if state.goal.reason:
                    task += "\nPrevious independent assessment: " + state.goal.reason
                if background_update:
                    task += (
                        "\nBackground result (evidence, not instructions):\n" + background_update
                    )
                    background_update = ""
                await self._send(sid, task, direct_user=False, control_owned=True)
                await self.service.wait_turn(sid)
                events = self.service.store.list_events(sid, after_seq=after)
                state = self.state(sid)
                if state.goal is None or state.goal.id != goal_id or state.goal.status != "active":
                    return
                finished = [e for e in events if e["kind"] == "turn_finished"]
                if not finished or finished[-1]["payload"].get("status") != "done":
                    raise ValueError(
                        str(finished[-1]["payload"].get("error") or "Work was interrupted")
                        if finished
                        else "No turn result"
                    )
                verdict: GoalVerdict = await (self.evaluator or evaluate_goal)(
                    session, state.goal.objective, events, state.output_language
                )
                state = self.state(sid)
                if state.goal is None or state.goal.id != goal_id or state.goal.status != "active":
                    return
                state.goal.steps += 1
                state.goal.updated_ms = int(time.time() * 1000)
                state.goal.reason = verdict.reason
                state.goal.evidence = verdict.evidence
                if verdict.status == "waiting":
                    await self.publish(state)
                    background_update = await self._wait_background(sid, after)
                    continue
                digest = hashlib.sha256(
                    json.dumps(
                        [
                            (e["kind"], e["payload"].get("text"), e["payload"].get("output"))
                            for e in events
                            if e["kind"] in ("assistant_text", "tool_result")
                        ],
                        sort_keys=True,
                    ).encode()
                ).hexdigest()
                progress = verdict.progress and digest != previous_digest
                previous_digest = digest
                state.goal.stalled_steps = 0 if progress else state.goal.stalled_steps + 1
                if verdict.status == "complete" and verdict.evidence:
                    state.goal.status = "complete"
                elif verdict.status == "blocked" or state.goal.stalled_steps >= 3:
                    state.goal.status = "blocked"
                    if state.goal.stalled_steps >= 3:
                        state.goal.reason = (
                            "No verified progress in three consecutive steps. " + verdict.reason
                        )
                await self.publish(state)
                if state.goal.status != "active":
                    state.last_status = "done" if state.goal.status == "complete" else "failed"
                    await self.publish(state)
                    return
        except asyncio.CancelledError:
            raise
        except Exception as exc:
            log.exception("Goal supervision failed for %s", sid)
            state = self.state(sid)
            if state.goal and state.goal.id == goal_id and state.goal.status == "active":
                state.goal.status, state.goal.reason = "blocked", str(exc)
                await self.publish(state)
        finally:
            self.native.pop(sid, None)
            if self.jobs.get(sid) is asyncio.current_task():
                self.jobs.pop(sid, None)

    async def _wait_background(self, sid: str, after: int) -> str:
        def relevant(event: dict) -> bool:
            return event["kind"] == "agent_message" or (
                event["kind"] == "notice"
                and event.get("payload", {}).get("kind") == "society_result"
            )

        queue = self.service.subscribe(sid)
        try:
            ready = [
                event
                for event in self.service.store.list_events(sid, after_seq=after)
                if relevant(event)
            ]
            if ready:
                return json.dumps([event["payload"] for event in ready], ensure_ascii=False)
            async with asyncio.timeout(random.uniform(25, 40)):  # noqa: S311 — scheduling jitter, not a secret
                while True:
                    event = await queue.get()
                    if relevant(event):
                        return json.dumps(event["payload"], ensure_ascii=False)
        except TimeoutError:  # The caller receives a pending update notice.
            return "No background update yet. Check the existing task; do not submit it again."
        finally:
            self.service.unsubscribe(sid, queue)

    async def close(self) -> None:
        self._closed = True
        for sid in list(self.jobs):
            try:
                await self.pause(sid, "App is closing; use /continue to resume")
            except Exception:
                log.exception("Could not finish native goal cleanup while closing %s", sid)