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.

26.1 KB
Raw Blame
"""/api/society: roster CRUD with derived focus, messaging, assign, board, controls."""

from __future__ import annotations

from pathlib import Path
from types import SimpleNamespace

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient

from dodaios.society.runtime import SocietyRuntime
from dodaios.ui.web.society_routes import router


class FakeManager:
    def __init__(self) -> None:
        self.prompts: list[str] = []
        self.cancelled: list[str] = []
        self.bus = SimpleNamespace(subscribe_all=lambda h: lambda: None)

    async def dispatch(self, *, prompt: str, **kwargs) -> str:
        self.prompts.append(prompt)
        return f"m-{len(self.prompts)}"

    async def cancel(self, mission_id: str) -> None:
        self.cancelled.append(mission_id)


def _tool(name: str, desc: str) -> SimpleNamespace:
    return SimpleNamespace(name=name, description=desc, risk_tier="monitor", schema={})


TOOLS = {
    "gmail": _tool("gmail", "Read and send mail."),
    "search-web": _tool("search-web", "Search the web."),
    "spawn-worker": _tool("spawn-worker", "Spawn."),
}


@pytest.fixture
def client(tmp_path: Path):
    manager = FakeManager()
    runtime = SocietyRuntime(
        tmp_path,
        seed_starter_team=False,
        mission_manager=lambda: manager,
        mission_bus=lambda: manager.bus,
        brain_tools=lambda: TOOLS,
    )
    app = FastAPI()
    app.include_router(router)
    app.state.society = None
    app.state.society_factory = lambda: runtime
    with TestClient(app) as c:
        try:
            yield c, manager
        finally:
            assert c.portal is not None
            c.portal.call(runtime.close)


def test_lead_is_seeded_and_listed(client):
    c, _ = client
    body = c.get("/api/society/agents").json()
    assert [a["agent_id"] for a in body["agents"]] == ["jarvis"]
    assert body["agents"][0]["tier"] == "lead"
    assert body["agents"][0]["run_state"] == "idle"


def test_group_membership_keeps_individual_agent_sessions(client):
    c, _ = client
    for name in ("Scout", "Planner", "Writer"):
        assert c.post("/api/society/agents", json={"name": name}).status_code == 200
    created = c.post(
        "/api/society/chat-groups",
        json={"name": "Launch team", "members": ["scout", "planner"]},
    )
    assert created.status_code == 200, created.text
    group = created.json()["group"]
    group_id = group["group_id"]
    assert c.get("/api/society/chat-groups").json()["groups"] == [group]
    assert (
        c.post(
            "/api/society/chat-groups", json={"name": "Too small", "members": ["scout"]}
        ).status_code
        == 422
    )
    before = {
        agent["agent_id"]: agent["session_id"]
        for agent in c.get("/api/society/agents").json()["agents"]
    }
    assert (
        c.patch(
            f"/api/society/chat-groups/{group_id}",
            json={"name": "Delivery team", "members": ["scout", "writer"]},
        ).status_code
        == 200
    )
    assert c.delete(f"/api/society/chat-groups/{group_id}").status_code == 200
    assert c.get("/api/society/chat-groups").json()["groups"] == []
    after = {
        agent["agent_id"]: agent["session_id"]
        for agent in c.get("/api/society/agents").json()["agents"]
    }
    assert after == before


def test_shared_meeting_routes_allow_jarvis_and_read_without_spend(client):
    c, _ = client
    c.post("/api/society/agents", json={"name": "Scout"})
    group = c.post("/api/society/chat-groups", json={
        "name": "With Jarvis", "members": ["jarvis", "scout"],
    }).json()["group"]
    url = f"/api/society/chat-groups/{group['group_id']}/meeting"
    assert c.get(url).json() == {"messages": [], "running": False, "room": None}
    assert c.post(url, json={"text": "Hello"}).status_code == 409  # No chat service.
    assert c.get(url).json()["messages"] == []


def test_deleting_active_meeting_stops_owned_turn_and_edits_are_refused(client, tmp_path):
    import asyncio

    from dodaios.agent_chat.store import AgentChatStore
    from tests.fakes.meeting_chat import MeetingChatFake

    c, _ = client
    for name in ("Scout", "Writer"):
        c.post("/api/society/agents", json={"name": name, "provider": "ollama", "model": "fake"})
    group = c.post("/api/society/chat-groups", json={
        "name": "Team", "members": ["scout", "writer"],
    }).json()["group"]
    rt = c.app.state.society
    svc = MeetingChatFake(AgentChatStore(tmp_path / "meeting-chat.db"))
    rt._get_chat = lambda: svc
    rt._get_cfg = lambda: SimpleNamespace(memory=SimpleNamespace(data_dir=str(tmp_path)))
    svc.hold = True
    path = f"/api/society/chat-groups/{group['group_id']}"
    try:
        assert c.post(f"{path}/meeting", json={"text": "Discuss"}).status_code == 200
        c.portal.call(svc.started.wait)
        edited = c.patch(path, json={"name": "Changed", "members": group["members"]})
        assert edited.status_code == 409

        async def release_soon():
            asyncio.get_running_loop().call_later(0.05, svc.release.set)

        c.portal.call(release_soon)
        assert c.delete(path).status_code == 200
        assert svc.cancelled == [("society:scout", "turn-0")]
        assert len(svc.sent) == 1
        assert c.get(f"{path}/meeting").status_code == 404
    finally:
        svc.release.set()
        c.portal.call(rt.meetings.stop, group["group_id"])
        svc.store.close()


def test_create_derives_focus_and_rules(client):
    c, _ = client
    res = c.post(
        "/api/society/agents",
        json={
            "name": "Mailbox",
            "title": "Gmail agent",
            "description": "You read and answer my mail; external mail only after approval.",
        },
    )
    assert res.status_code == 200, res.text
    agent = res.json()["agent"]
    assert res.json()["created"] is True
    assert agent["focus"] == ["plugin:gmail"]
    assert agent["approval_rules"]["require_approval"] == ["plugin:gmail:send"]
    assert agent["session_id"] == "society:mailbox"
    # Adopt-before-mint through REST.
    again = c.post("/api/society/agents", json={"name": "mailbox"}).json()
    assert again["created"] is False


def test_patch_preserves_and_extends_focus_on_description_change(client):
    c, _ = client
    c.post("/api/society/agents", json={"name": "Scout", "description": "Research on the web."})
    first = c.get("/api/society/agents/scout").json()["agent"]
    assert first["focus"] == ["core:search-web"]
    patched = c.patch(
        "/api/society/agents/scout", json={"description": "Handle my mail inbox."}
    ).json()["agent"]
    assert patched["focus"] == ["core:search-web", "plugin:gmail"]
    explicit = c.patch("/api/society/agents/scout", json={"focus": []}).json()["agent"]
    assert explicit["focus"] == []


def test_rename_agent_keeps_chat_identity_and_rejects_duplicates(client):
    c, _ = client
    c.post("/api/society/agents", json={"name": "Scout"})
    c.post("/api/society/agents", json={"name": "Planner"})
    response = c.patch("/api/society/agents/scout", json={"name": "Research Scout"})
    assert response.status_code == 200
    agent = response.json()["agent"]
    assert agent["agent_id"] == "scout"
    assert agent["session_id"] == "society:scout"
    assert agent["name"] == "Research Scout"
    assert c.patch("/api/society/agents/scout", json={"name": "Planner"}).status_code == 409
    assert c.patch("/api/society/agents/jarvis", json={"name": "Other Lead"}).status_code == 409


def test_typed_errors(client):
    c, _ = client
    assert c.get("/api/society/agents/ghost").status_code == 404
    bad = c.post("/api/society/agents", json={"name": "Boss", "tier": "lead"})
    assert bad.status_code == 409
    assert bad.json()["detail"]["reason"] == "tier_not_allowed"
    assert c.delete("/api/society/agents/jarvis").status_code == 409
    c.post("/api/society/agents", json={"name": "Scout"})
    worse = c.patch("/api/society/agents/scout", json={"permission_ceiling": "block"})
    assert worse.status_code == 409


def test_message_lands_in_inbox_and_board(client):
    c, _ = client
    c.post("/api/society/agents", json={"name": "Scout"})
    res = c.post("/api/society/agents/scout/message", json={"text": "hello"})
    assert res.status_code == 200
    event = res.json()["event"]
    assert event["msg_type"] == "SAY" and event["from_agent"] == "user"
    inbox = c.get("/api/society/agents/scout/inbox").json()["events"]
    assert [e["payload"]["text"] for e in inbox] == ["hello"]
    board = c.get("/api/society/events").json()
    assert board["last_seq"] == 1
    assert c.get("/api/society/events", params={"trace_id": event["trace_id"]}).json()["events"]


def test_assign_dispatches_a_framed_mission(client):
    c, manager = client
    c.post(
        "/api/society/agents",
        json={"name": "Scout", "title": "Research scout", "description": "Find things on the web."},
    )
    res = c.post("/api/society/agents/scout/assign", json={"task": "Find the best VPS."})
    assert res.status_code == 200, res.text
    body = res.json()
    assert body["outcome"]["msg_type"] == "CLAIM"
    assert body["outcome"]["payload"]["run_id"] == "m-1"
    prompt = manager.prompts[0]
    assert prompt.startswith("You are Scout, Research scout,")
    assert "Find things on the web." in prompt and "Find the best VPS." in prompt
    assert "core:search-web" in prompt
    status = c.get("/api/society/status").json()
    assert status["active_runs"] == 1 and status["running"] == {"m-1": "scout"}
    listed = c.get("/api/society/agents").json()["agents"]
    assert next(a for a in listed if a["agent_id"] == "scout")["run_state"] == "working"


def test_assign_from_specialist_is_vetoed(client):
    c, manager = client
    c.post("/api/society/agents", json={"name": "Scout"})
    c.post("/api/society/agents", json={"name": "Quill"})
    res = c.post("/api/society/agents/quill/assign", json={"task": "x", "from_agent": "scout"})
    assert res.json()["outcome"]["msg_type"] == "VETO"
    assert res.json()["outcome"]["payload"]["reason"] == "tier_not_allowed"
    assert manager.prompts == []


def test_kill_agent_drops_runs_and_pauses(client):
    c, manager = client
    c.post("/api/society/agents", json={"name": "Scout"})
    c.post("/api/society/agents/scout/assign", json={"task": "go"})
    res = c.post("/api/society/agents/scout/kill")
    assert res.json()["runs_dropped"] == 1
    assert res.json()["agent"]["state"] == "paused"
    assert manager.cancelled == ["m-1"]


def test_kill_switch_round_trip(client):
    c, _ = client
    c.post("/api/society/agents", json={"name": "Scout"})
    on = c.post("/api/society/kill-switch").json()
    assert on["engaged"] is True
    assert c.get("/api/society/status").json()["kill_switch"] is True
    vetoed = c.post("/api/society/agents/scout/assign", json={"task": "x"}).json()
    assert vetoed["outcome"]["payload"]["reason"] == "kill_switch"
    assert c.post("/api/society/kill-switch/release").json()["engaged"] is False


def test_capabilities_catalog_hides_dispatch(client):
    c, _ = client
    ids = [r["id"] for r in c.get("/api/society/capabilities").json()["capabilities"]]
    assert ids == ["plugin:gmail", "core:browser", "core:coding-session", "core:search-web"]


def test_rooms_over_rest(client):
    c, _ = client
    c.post("/api/society/agents", json={"name": "Scout"})
    c.post("/api/society/agents", json={"name": "Quill"})
    missing = c.post("/api/society/rooms", json={"members": ["scout", "ghost"]})
    assert missing.status_code == 404
    room = c.post("/api/society/rooms", json={"members": ["scout", "quill"], "topic": "t"}).json()[
        "room"
    ]
    assert room["next_speaker"] == "scout"
    wrong = c.post(
        f"/api/society/rooms/{room['room_id']}/say", json={"member": "quill", "text": "me"}
    )
    assert wrong.status_code == 409
    ok = c.post(f"/api/society/rooms/{room['room_id']}/say", json={"member": "scout", "text": "hi"})
    assert ok.json()["room"]["message_count"] == 1
    settled = c.post(f"/api/society/rooms/{room['room_id']}/settle").json()["room"]
    assert settled["state"] == "settled"
    assert c.get("/api/society/rooms").json()["rooms"][0]["room_id"] == room["room_id"]


def test_missing_factory_is_503():
    app = FastAPI()
    app.include_router(router)
    with TestClient(app) as c:
        assert c.get("/api/society/status").status_code == 503


def test_approvals_over_rest(client):
    c, _ = client
    c.post("/api/society/agents", json={"name": "Mailbox"})
    created = c.post(
        "/api/society/approvals",
        json={
            "agent_id": "mailbox",
            "trace_id": "t",
            "capability": "plugin:gmail",
            "action": {"verb": "send"},
            "summary": "Send the report",
        },
    )
    assert created.status_code == 200, created.text
    item = created.json()["approval"]
    assert item["state"] == "pending"
    listed = c.get("/api/society/approvals").json()
    assert [a["id"] for a in listed["approvals"]] == [item["id"]]
    assert c.get("/api/society/approvals", params={"agent_id": "nobody"}).json()["total"] == 0
    board = c.get("/api/society/events", params={"trace_id": "t"}).json()["events"]
    assert [e["msg_type"] for e in board] == ["HOLD"]
    res = c.post(
        f"/api/society/approvals/{item['id']}/resolve", json={"approve": True, "note": "ok"}
    )
    assert res.json()["approval"]["state"] == "approved"
    assert c.get("/api/society/approvals").json()["total"] == 0
    assert c.post("/api/society/approvals/nope/resolve", json={"approve": False}).status_code == 404
    assert c.post("/api/society/approvals/resurface").json()["total"] == 0
    assert (
        c.post("/api/society/approvals", json={"agent_id": "ghost", "capability": "x"}).status_code
        == 404
    )


def test_routines_over_rest(client):
    from tests.unit.society.test_routines import FakeScheduler, FakeTaskStore

    c, _ = client
    c.post("/api/society/agents", json={"name": "Mailbox", "title": "Gmail agent"})
    assert c.get("/api/society/agents/mailbox/routines").status_code == 503
    store = FakeTaskStore()
    c.app.state.task_store = store
    c.app.state.task_scheduler = FakeScheduler(store)
    res = c.post(
        "/api/society/agents/mailbox/routines",
        json={"title": "Morning brief", "prompt": "Summarize mail.", "schedule": {"kind": "every"}},
    )
    assert res.status_code == 200, res.text
    assert res.json()["title"] == "[agent:Mailbox] Morning brief"
    assert res.json()["tags"] == ["society", "agent:mailbox"]
    listed = c.get("/api/society/agents/mailbox/routines").json()
    assert listed["total"] == 1 and listed["routines"][0]["state"] == "scheduled"
    bad = c.post(
        "/api/society/agents/mailbox/routines",
        json={"title": "x", "prompt": "p", "schedule": {"kind": "cron"}},
    )
    assert bad.status_code == 422
    assert c.get("/api/society/agents/ghost/routines").status_code == 404


def test_retiring_an_agent_pauses_its_routines(client):
    from tests.unit.society.test_routines import FakeScheduler, FakeTaskStore

    c, _ = client
    c.post("/api/society/agents", json={"name": "Mailbox", "title": "Gmail agent"})
    store = FakeTaskStore()
    c.app.state.task_store = store
    c.app.state.task_scheduler = FakeScheduler(store)
    for kind in ("every", "webhook"):
        res = c.post(
            "/api/society/agents/mailbox/routines",
            json={"title": f"Brief {kind}", "prompt": "Summarize.", "schedule": {"kind": kind}},
        )
        assert res.status_code == 200, res.text
    assert c.delete("/api/society/agents/mailbox").status_code == 200
    listed = c.get("/api/society/agents/mailbox/routines").json()["routines"]
    assert [r["state"] for r in listed] == ["paused", "paused"]
    # A retired agent runs, resumes and gains nothing until it is brought back.
    tid = listed[0]["id"]
    for operation in ("run", "resume"):
        res = c.post(
            f"/api/society/agents/mailbox/routines/{tid}/operation",
            json={"operation": operation},
        )
        assert res.status_code == 409, (operation, res.text)
    res = c.post(
        "/api/society/agents/mailbox/routines",
        json={"title": "Again", "prompt": "Summarize.", "schedule": {"kind": "every"}},
    )
    assert res.status_code == 409
    after = c.get("/api/society/agents/mailbox/routines").json()["routines"]
    assert [r["state"] for r in after] == ["paused", "paused"]


def test_browser_routes(client, tmp_path, monkeypatch):
    c, _ = client
    status = c.get("/api/society/browser/status").json()
    assert status["installed"] is False and status["phase"] == "idle"
    c.post("/api/society/agents", json={"name": "Scout"})
    per_agent = c.get("/api/society/agents/scout/browser").json()
    assert per_agent["installed"] is False and per_agent["mode"] == "own"
    assert per_agent["logged_in_profile"] is None  # An unopened profile has unverified login state.
    # On-demand setup failures remain typed; this unit test never provisions a runtime.
    from dodaios.society.browser import install as install_mod

    def unavailable(*_args, **_kwargs):
        raise RuntimeError("Fixture browser setup is unavailable")

    monkeypatch.setattr(install_mod, "ensure_installed", unavailable)
    refused = c.post("/api/society/agents/scout/browser/login", json={})
    assert refused.status_code == 409
    assert refused.json()["detail"]["reason"] == "target_busy"
    assert c.post("/api/society/agents/scout/browser/login/done").json()["closed"] is False
    # Attach mode is a roster field with its own check.
    patched = c.patch("/api/society/agents/scout", json={"browser_mode": "attach"}).json()
    assert patched["agent"]["browser_mode"] == "attach"
    assert c.patch("/api/society/agents/scout", json={"browser_mode": "cloud"}).status_code == 409
    domains = c.patch(
        "/api/society/agents/scout", json={"browser_allowed_domains": ["Mail.Google.com", "x.com"]}
    ).json()
    assert domains["agent"]["browser_allowed_domains"] == ["mail.google.com", "x.com"]
    # Install kicks off a background thread; a stub keeps the test offline.
    from dodaios.society.browser import install as install_mod

    monkeypatch.setattr(install_mod, "start_install", lambda data_dir=None: (True, "stubbed"))
    started = c.post("/api/society/browser/install").json()
    assert started["started"] is True and started["message"] == "stubbed"


# ------------------------------------------------------------------- memory


@pytest.fixture
def memory_client(tmp_path: Path):
    cfg = SimpleNamespace(
        wiki=SimpleNamespace(vault_root=str(tmp_path / "vault")),
        memory=SimpleNamespace(data_dir=str(tmp_path / "data")),
    )
    runtime = SocietyRuntime(tmp_path, seed_starter_team=False, cfg=lambda: cfg)
    app = FastAPI()
    app.include_router(router)
    app.state.society = None
    app.state.society_factory = lambda: runtime
    with TestClient(app) as c:
        yield c, tmp_path / "vault"


def test_memory_overview_recall_and_promotion_through_approvals(memory_client):
    c, vault = memory_client
    c.post("/api/society/agents", json={"name": "Scout"})
    # A legacy shared proposal remains manageable by the person after agent sharing stops.
    import asyncio

    runtime: SocietyRuntime = c.app.state.society
    scout = asyncio.run(runtime.roster.get("scout"))
    assert scout is not None
    got = asyncio.run(
        runtime.memory.propose_shared(scout, "Hosting", "We host on Hetzner.", root=vault)
    )
    view = c.get("/api/society/memory").json()
    assert view["shared"] == []
    assert [u["id"] for u in view["unreviewed"]] == [got["knowledge_id"]]
    assert next(a for a in view["agents"] if a["agent_id"] == "scout")["notes"] == 1
    # The proposal waits for the person: the figure stands at the gate (rule 2 beats rule 4).
    assert c.get("/api/society/agents/scout").json()["agent"]["checkpoint"] == "gate"
    # Ordinary recall cannot expose another agent's notes, even to the society lead.
    hits = c.post("/api/society/memory/recall", json={"query": "Hetzner hosting"}).json()["hits"]
    assert hits == []
    own = c.post(
        "/api/society/memory/recall",
        json={"query": "Hetzner hosting", "agent_id": "scout"},
    ).json()["hits"]
    assert own and all(hit["scope"] == "own" for hit in own)
    # Approving the share item promotes the page.
    approvals = c.get("/api/society/approvals").json()["approvals"]
    assert approvals[0]["capability"] == "core:memory:share"
    res = c.post(f"/api/society/approvals/{approvals[0]['id']}/resolve", json={"approve": True})
    assert res.status_code == 200, res.text
    assert res.json()["promoted"] == "society/shared/hosting.md"
    assert (vault / "society" / "shared" / "hosting.md").is_file()
    view = c.get("/api/society/memory").json()
    assert view["shared"][0]["title"] == "Hosting" and view["unreviewed"] == []
    hits = c.post("/api/society/memory/recall", json={"query": "Hetzner"}).json()["hits"]
    assert hits == []
    # Direct promote/dismiss on unknown rows answer 404.
    assert c.post("/api/society/memory/999/promote").status_code == 404
    assert c.post("/api/society/memory/999/dismiss").status_code == 404


def test_memory_file_serves_society_page_and_refuses_escape(memory_client):
    c, vault = memory_client
    c.post("/api/society/agents", json={"name": "Scout"})
    import asyncio

    runtime: SocietyRuntime = c.app.state.society
    scout = asyncio.run(runtime.roster.get("scout"))
    rel = asyncio.run(runtime.memory.remember(scout, "Loves maps.", root=vault))
    assert rel == "society/scout/MEMORY.md"
    got = c.get("/api/society/memory/file", params={"path": rel}).json()
    assert got["path"] == rel and got["agent_id"] == "scout"
    assert "Loves maps." in got["content"]
    # Outside society/ is never served, even with traversal.
    assert c.get("/api/society/memory/file", params={"path": "jarvis.toml"}).status_code == 404
    traversal = c.get("/api/society/memory/file", params={"path": "society/../jarvis.toml"})
    assert traversal.status_code == 404
    missing = c.get("/api/society/memory/file", params={"path": "society/scout/missing.md"})
    assert missing.status_code == 404


def test_bind_agent_chat_creates_the_canonical_session(tmp_path: Path):
    from dodaios.agent_chat.store import AgentChatStore

    class FakeService:
        def __init__(self, store: AgentChatStore) -> None:
            self.store = store

        def is_running(self, session_id: str) -> bool:
            return False

    svc = FakeService(AgentChatStore(tmp_path / "chat.db"))
    cfg = SimpleNamespace(
        memory=SimpleNamespace(data_dir=str(tmp_path / "data")),
        wiki=SimpleNamespace(vault_root=str(tmp_path / "vault")),
    )
    runtime = SocietyRuntime(
        tmp_path, seed_starter_team=False, chat_service=lambda: svc, cfg=lambda: cfg
    )
    app = FastAPI()
    app.include_router(router)
    app.state.society = None
    app.state.society_factory = lambda: runtime
    with TestClient(app) as c:
        c.post(
            "/api/society/agents", json={"name": "Scout", "provider": "openai", "model": "gpt-5"}
        )
        res = c.post("/api/society/agents/scout/chat")
        assert res.status_code == 200, res.text
        session = res.json()["session"]
        assert session["session_id"] == "society:scout" and session["surface"] == "society"
        assert session["provider"] == "openai" and session["model"] == "gpt-5"
        # Idempotent: the same session comes back, nothing is duplicated.
        again = c.post("/api/society/agents/scout/chat").json()["session"]
        assert again["session_id"] == session["session_id"]
        assert c.post("/api/society/agents/nobody/chat").status_code == 404
    svc.store.close()


def test_bind_agent_chat_without_a_chat_service_is_503(memory_client):
    c, _ = memory_client
    c.post("/api/society/agents", json={"name": "Scout"})
    assert c.post("/api/society/agents/scout/chat").status_code == 503


def test_pausing_moves_the_figure_home(memory_client):
    c, _ = memory_client
    c.post("/api/society/agents", json={"name": "Scout"})
    runtime: SocietyRuntime = c.app.state.society
    import asyncio

    asyncio.run(
        runtime.approvals.enqueue(
            agent_id="scout", trace_id="t", capability="x", action={}, summary="x"
        )
    )
    assert c.get("/api/society/agents/scout").json()["agent"]["checkpoint"] == "gate"
    res = c.patch("/api/society/agents/scout", json={"state": "paused"})
    assert res.json()["agent"]["checkpoint"] == "idle"


def test_a_runtime_agent_on_an_unsupported_provider_names_a_translatable_reason(client):
    c, _ = client
    res = c.post(
        "/api/society/agents",
        json={"name": "Rune", "runtime": "hermes", "provider": "no-such-vendor", "model": "x"},
    )
    assert res.status_code == 422
    detail = res.json()["detail"]
    # The UI shows its own sentence for the reason; the detail names every way that works.
    assert detail["reason"] == "runtime_provider_unsupported"
    assert "ChatGPT subscription" in detail["detail"]


def test_agent_rows_say_whether_the_seat_pays_per_token(client, monkeypatch):
    from dodaios.society import routine_runner

    c, _ = client
    c.post("/api/society/agents", json={"name": "Mailbox", "title": "Gmail agent"})
    monkeypatch.setattr(routine_runner, "_billed_via_api", lambda provider, account_id="": False)
    rows = {a["agent_id"]: a for a in c.get("/api/society/agents").json()["agents"]}
    # No own seat (the default brain): its costs stay visible.
    assert rows["mailbox"]["billed_per_token"] is True
    from dodaios.ui.web import society_routes

    seat = SimpleNamespace(provider="claude-api", account_id="subscription")
    assert society_routes._billed_per_token(seat) is False


def test_an_agent_without_its_own_seat_follows_the_default_model(monkeypatch):
    from dodaios.society import chat_binding, routine_runner
    from dodaios.ui.web import society_routes

    monkeypatch.setattr(chat_binding, "pair_for", lambda cfg, agent: ("codex-cli", "gpt", ""))
    monkeypatch.setattr(
        routine_runner, "_billed_via_api", lambda provider, account_id="": provider != "codex-cli"
    )
    seatless = SimpleNamespace(provider="", account_id="")
    # The default model is a subscription: no dollar lines for this agent.
    assert society_routes._billed_per_token(seatless, object()) is False
    # Without a config the costs stay visible.
    assert society_routes._billed_per_token(seatless) is True