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.

50.1 KB
Raw Blame
"""Team leads: leadership, the goal loop, waiting, stopping, recovery, the tools."""

from __future__ import annotations

import asyncio
import json
from collections.abc import Callable
from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace
from typing import Any

import aiosqlite
import pytest

from dodaios.agent_chat.store import AgentChatStore
from dodaios.core.protocols import ChatTurn, current_chat_turn
from dodaios.society.events import MsgType, SocietyEnvelope, TeamGoalState, TeamTaskState, Tier
from dodaios.society.lead_card import render_lead_card
from dodaios.society.runtime import SocietyRuntime
from dodaios.society.store import SocietyStore
from dodaios.society.team_lead import (
    INITIATIVE_PROMPT,
    MAX_LEAD_TURNS,
    TRACE_PREFIX,
    TeamLeadError,
    goal_title,
)
from dodaios.society.team_tools import (
    TEAM_ASSIGN_TOOL_NAME,
    TEAM_BOARD_TOOL_NAME,
    TEAM_REPORT_TOOL_NAME,
    TeamAssignTool,
    TeamReportTool,
    team_lead_tools,
)
from tests.fakes.team_chat import FakeTasks, TeamChatFake

LEAD = "society:boss"
SCOUT = "society:scout"
WRITER = "society:writer"


async def until(predicate: Callable[[], Any], *, within: float = 5.0) -> None:
    """Poll an awaitable-or-plain predicate until it holds."""
    loop = asyncio.get_running_loop()
    deadline = loop.time() + within
    while True:
        value = predicate()
        if asyncio.iscoroutine(value):
            value = await value
        if value:
            return
        if loop.time() > deadline:
            raise AssertionError("condition was not reached in time")
        await asyncio.sleep(0.01)


def _runtime(tmp_path: Path, svc: TeamChatFake) -> SocietyRuntime:
    cfg = SimpleNamespace(memory=SimpleNamespace(data_dir=str(tmp_path)))
    rt = SocietyRuntime(tmp_path, chat_service=lambda: svc, cfg=lambda: cfg)
    leads = rt.team_leads
    leads._retry_delay = 0.02  # noqa: SLF001 — tests do not wait fifteen seconds
    leads._review_delay = 0.0  # noqa: SLF001
    leads._lead_retry = 0.02  # noqa: SLF001
    return rt


@pytest.fixture
async def team(tmp_path: Path):
    svc = TeamChatFake(AgentChatStore(tmp_path / "chat.db"))
    rt = _runtime(tmp_path, svc)
    await rt.ensure_started()
    for name in ("Boss", "Scout", "Writer"):
        await rt.roster.create(name=name, provider="ollama", model="fake")
    await rt.store.create_chat_group("team", "Launch", ["boss", "scout", "writer"])
    await rt.team_leads.set_lead("team", "boss")
    try:
        yield rt, svc
    finally:
        await svc.close()
        await rt.close()
        svc.store.close()


async def _goal(rt: SocietyRuntime, goal_id: str):
    goal = await rt.team_leads.get_goal(goal_id)
    assert goal is not None
    return goal


def _state_is(rt: SocietyRuntime, goal_id: str, state: TeamGoalState) -> Callable[[], Any]:
    async def check() -> bool:
        return (await _goal(rt, goal_id)).state is state

    return check


# ------------------------------------------------------------- leadership


def test_goal_title_is_the_first_line_trimmed():
    assert goal_title("Launch the page\nby Friday") == "Launch the page"
    assert goal_title("x" * 200).endswith("…")
    assert goal_title("anything", "Given") == "Given"
    assert goal_title("  ") == "Goal"


async def test_naming_a_lead_promotes_it_and_clearing_restores_it(team):
    rt, _ = team
    boss = await rt.roster.get("boss")
    assert boss.tier is Tier.ORCHESTRATOR and boss.may_assign
    assert boss.max_concurrent_runs == 3
    assert rt.team_leads.group_led_by("boss") == "team"
    snapshot = await rt.team_leads.snapshot("team")
    assert snapshot["lead_agent_id"] == "boss" and snapshot["lead_available"]

    await rt.team_leads.set_lead("team", None)
    boss = await rt.roster.get("boss")
    assert boss.tier is Tier.SPECIALIST and boss.max_concurrent_runs == 1
    assert rt.team_leads.group_led_by("boss") is None


async def test_an_agent_that_was_an_orchestrator_before_stays_one(team):
    rt, _ = team
    await rt.roster.create(name="Chief", tier=Tier.ORCHESTRATOR, provider="ollama", model="fake")
    await rt.team_leads.set_lead("team", None)
    await rt.team_leads.set_lead("team", "chief")
    assert "chief" in (await rt.store.get_chat_group("team"))["members"]
    await rt.team_leads.set_lead("team", None)
    assert (await rt.roster.get("chief")).tier is Tier.ORCHESTRATOR


async def test_jarvis_and_a_lead_of_another_team_are_refused(team):
    rt, _ = team
    with pytest.raises(TeamLeadError, match="already leads every agent"):
        await rt.team_leads.set_lead("team", "jarvis")
    await rt.store.create_chat_group("other", "Other", ["boss", "scout"])
    with pytest.raises(TeamLeadError, match="another team"):
        await rt.team_leads.set_lead("other", "boss")
    with pytest.raises(TeamLeadError) as missing:
        await rt.team_leads.set_lead("nope", "scout")
    assert missing.value.status == 404


async def test_create_lead_makes_a_new_orchestrator_with_a_free_name(team):
    rt, _ = team
    await rt.store.create_chat_group("ops", "Ops", ["scout", "writer"])
    group, agent = await rt.team_leads.create_lead(
        "ops", fields={"provider": "ollama", "model": "fake"}
    )
    assert agent.name == "Ops Lead" and agent.tier is Tier.ORCHESTRATOR
    assert group["lead_agent_id"] == agent.agent_id and agent.agent_id in group["members"]
    assert "manage a team" in agent.description
    await rt.store.create_chat_group("ops2", "Ops", ["scout", "writer"])
    _, second = await rt.team_leads.create_lead("ops2")
    assert second.name == "Ops Lead 2"


async def test_a_lead_leaving_the_team_stops_leading_it(team):
    rt, _ = team
    await rt.team_leads.members_changed("team", ["scout", "writer"])
    assert (await rt.store.get_chat_group("team"))["lead_agent_id"] is None
    assert rt.team_leads.group_led_by("boss") is None


# ------------------------------------------------------------ the goal loop


async def test_a_goal_is_planned_worked_reviewed_and_reported(team):
    rt, svc = team
    leads = rt.team_leads
    svc.replies[SCOUT] = "Three competitors found: A, B, C."
    svc.replies[WRITER] = "Draft is in draft.md."
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if text.startswith("[team goal for"):
            await leads.assign("boss", member="Scout", title="Research", brief="Find competitors.")
            await leads.assign("boss", member="writer", title="Draft", brief="Write the page.")
            return "Two tasks are running."
        board = await leads.board("boss")
        tasks = board["goals"][0]["tasks"]
        if all(task["state"] == "done" for task in tasks):
            await leads.report("boss", status="done", summary="Launch page is ready.")
        return "Reviewed."

    svc.scripts[LEAD] = boss
    await rt.store.set_chat_group_mission("team", "Ship the launch.")
    goal = await leads.start_goal("team", "Launch the page\nby Friday", lang="en")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))

    done = await _goal(rt, goal.goal_id)
    assert done.report == "Launch page is ready." and done.title == "Launch the page"
    tasks = await leads.tasks(goal.goal_id)
    assert [(t.agent_id, t.state) for t in tasks] == [
        ("scout", TeamTaskState.DONE),
        ("writer", TeamTaskState.DONE),
    ]
    assert all(t.reviewed and t.trace_id.startswith(TRACE_PREFIX) for t in tasks)
    assert tasks[0].result == "Three competitors found: A, B, C."

    assert "Team mission: Ship the launch." in seen[0]
    assert "- Scout" in seen[0] and "- Writer" in seen[0] and "Boss" not in seen[0]
    reviews = "\n".join(seen[1:])
    assert "[team update for team Launch]" in reviews
    assert "Three competitors found" in reviews and "Draft is in draft.md." in reviews
    # The lead's turns are not messages the person typed into its chat.
    lead_calls = [call for call in svc.sent if call[0] == LEAD]
    assert all(call[2]["direct_user"] is False for call in lead_calls)
    assert lead_calls[0][2]["output_language"] == "en"
    assert lead_calls[0][2]["display_text"] == "Launch the page\nby Friday"
    # Members got a framed assignment from the lead, on the board.
    assert svc.prompts(SCOUT)[0].startswith("[assignment from boss]")
    assert not rt.scheduler.running


async def test_an_answer_without_open_work_is_the_report(team):
    rt, svc = team
    svc.replies[LEAD] = "It is 42."
    goal = await rt.team_leads.start_goal("team", "What is the answer?")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert (await _goal(rt, goal.goal_id)).report == "It is 42."
    # A plain answer may be a result or a question: the lead is asked once for
    # a typed report, and only then is its answer taken as the result.
    prompts = svc.prompts(LEAD)
    assert len(prompts) == 2 and prompts[1].startswith("[team goal check for team Launch]")
    assert "society_team_report" in prompts[1]


async def test_the_view_hears_when_the_turn_that_closed_a_goal_ends(team):
    rt, svc = team
    leads = rt.team_leads
    pushed: list[tuple[str, bool]] = []
    leads._publish = lambda event: pushed.append((event.reason, bool(leads._turns)))  # noqa: SLF001

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        await leads.report("boss", status="done", summary="Done.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Do it")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    # The goal closed while its turn still ran; the last push comes once the
    # turn is over, so no view keeps showing the lead at work.
    await until(lambda: pushed and pushed[-1] == ("turn", False))
    assert (await leads.snapshot("team"))["working"] is False


async def test_results_arriving_during_a_lead_turn_reach_the_next_one(team):
    rt, svc = team
    leads = rt.team_leads
    release = asyncio.Event()
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if len(seen) == 1:
            await leads.assign("boss", member="scout", title="Research", brief="Look it up.")
            await release.wait()  # the member finishes while this turn is still open
            return "Assigned."
        await leads.report("boss", status="done", summary="All in.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Find it")

    async def scout_done() -> bool:
        tasks = await leads.tasks(goal.goal_id)
        return bool(tasks) and tasks[0].state is TeamTaskState.DONE

    await until(scout_done)
    assert len(seen) == 1
    release.set()
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert len(seen) == 2 and "New results:" in seen[1]


async def test_a_busy_member_is_knocked_on_again_instead_of_failing(team):
    rt, svc = team
    leads = rt.team_leads
    svc.busy.add(SCOUT)
    receipts: list[dict[str, Any]] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        if text.startswith("[team goal for"):
            receipts.append(
                await leads.assign("boss", member="scout", title="Research", brief="Go.")
            )
            return "Waiting on Scout."
        await leads.report("boss", status="done", summary="Done.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Research it")

    async def waiting() -> bool:
        tasks = await leads.tasks(goal.goal_id)
        return bool(tasks) and tasks[0].state is TeamTaskState.WAITING and tasks[0].attempts >= 2

    await until(waiting)
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.RUNNING

    # A long wait outlives the scheduler's per-trace message cap: every knock
    # is on its own trace.
    async def knocked_often() -> bool:
        return (await leads.tasks(goal.goal_id))[0].attempts >= 16

    await until(knocked_often)
    task = (await leads.tasks(goal.goal_id))[0]
    assert task.state in (TeamTaskState.WAITING, TeamTaskState.ASSIGNED)
    assert task.trace_id.count(":") == 3
    svc.busy.discard(SCOUT)
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert (await leads.tasks(goal.goal_id))[0].state is TeamTaskState.DONE


async def test_a_blocked_member_reaches_the_lead_as_blocked(team):
    rt, svc = team
    leads = rt.team_leads
    svc.failing.add(SCOUT)
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if len(seen) == 1:
            await leads.assign("boss", member="scout", title="Research", brief="Go.")
            return "Assigned."
        await leads.report("boss", status="failed", summary="Scout cannot reach the source.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Research it")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.FAILED))
    assert '"Research": blocked' in seen[1]
    assert (await _goal(rt, goal.goal_id)).report == "Scout cannot reach the source."


async def test_a_question_waits_for_the_person_and_their_answer_continues(team):
    rt, svc = team
    leads = rt.team_leads
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if len(seen) == 1:
            await leads.report("boss", status="needs_input", summary="Which market?")
            return "I need one answer."
        await leads.report("boss", status="done", summary="Planned for Germany.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Plan the launch")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.NEEDS_INPUT))
    await until(svc.idle)
    waiting = await _goal(rt, goal.goal_id)
    assert waiting.report == "Which market?" and waiting.note == "question"
    assert len(seen) == 1

    await leads.tell(goal.goal_id, "Germany")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert "The person says about this goal:" in seen[1] and "Germany" in seen[1]
    # Both reports are cards in the lead's chat, where the person reads along.
    def reports() -> list[dict[str, Any]]:
        return [n for n in svc.notices(LEAD) if n["kind"] == "team_report"]

    await until(lambda: len(reports()) == 2)
    cards = reports()
    assert [(n["status"], n["text"]) for n in cards] == [
        ("needs_input", "Which market?"),
        ("done", "Planned for Germany."),
    ]
    assert cards[0]["title"] == "Plan the launch" and cards[0]["agent_name"] == "Boss"
    with pytest.raises(TeamLeadError, match="not in progress"):
        await leads.tell(goal.goal_id, "More")


async def test_a_managing_turn_is_marked_so_no_review_follows_it(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[LEAD] = asyncio.Event()
    queued: list[str] = []
    rt.conversations.queue_review = lambda session_id, *a, **k: queued.append(session_id)  # type: ignore[method-assign]
    goal = await leads.start_goal("team", "Do it")
    await until(lambda: LEAD in svc.busy)
    assert leads.is_managing(LEAD) and not leads.is_managing(SCOUT)
    turn = SimpleNamespace(turn_id="t", direct_user=False)
    done = '[{"kind": "turn_finished", "payload": {"status": "done"}}]'
    completion = SimpleNamespace(events_json=done, turn=turn)
    await rt.turn_completed(SimpleNamespace(session_id=LEAD), completion)
    assert queued == []
    # The same agent's own work is still reviewed.
    await rt.turn_completed(SimpleNamespace(session_id=SCOUT), completion)
    assert queued == [SCOUT]
    svc.gates[LEAD].set()
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert not leads.is_managing(LEAD)


async def test_goals_queue_and_start_one_after_another(team):
    rt, svc = team
    leads = rt.team_leads
    gate = asyncio.Event()
    svc.gates[LEAD] = gate
    first = await leads.start_goal("team", "First")
    second = await leads.start_goal("team", "Second")
    await until(lambda: len(svc.prompts(LEAD)) == 1)
    assert (await _goal(rt, second.goal_id)).state is TeamGoalState.QUEUED
    gate.set()
    await until(_state_is(rt, second.goal_id, TeamGoalState.DONE))
    assert (await _goal(rt, first.goal_id)).state is TeamGoalState.DONE
    plans = [text for text in svc.prompts(LEAD) if text.startswith("[team goal for")]
    assert len(plans) == 2 and "First" in plans[0] and "Second" in plans[1]


async def test_the_lead_busy_in_its_own_chat_is_retried(team):
    rt, svc = team
    svc.busy.add(LEAD)
    goal = await rt.team_leads.start_goal("team", "Do it")
    await asyncio.sleep(0.1)
    assert svc.prompts(LEAD) == []
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.RUNNING
    svc.busy.discard(LEAD)
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))


# ------------------------------------------------------ stopping and bounds


async def test_cancel_stops_the_member_turn_and_closes_the_tasks(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[SCOUT] = asyncio.Event()

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        await leads.assign("boss", member="scout", title="Research", brief="Go.")
        return "Assigned."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Research it")

    async def running() -> bool:
        tasks = await leads.tasks(goal.goal_id)
        return bool(tasks) and tasks[0].state is TeamTaskState.RUNNING and svc.idle() is False

    await until(running)
    await until(lambda: LEAD not in svc.busy)
    cancelled = await leads.cancel(goal.goal_id)
    assert cancelled.state is TeamGoalState.CANCELLED
    assert (await leads.tasks(goal.goal_id))[0].state is TeamTaskState.CANCELLED
    assert (SCOUT, svc.sent[-1][3]) in svc.cancelled
    assert not rt.scheduler.running
    await until(svc.idle)
    # The stopped turn's late RESULT changes nothing.
    await asyncio.sleep(0.05)
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.CANCELLED


async def test_the_kill_switch_pauses_goals_and_resume_continues(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[SCOUT] = asyncio.Event()
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if len(seen) == 1:
            await leads.assign("boss", member="scout", title="Research", brief="Go.")
            return "Assigned."
        await leads.report("boss", status="done", summary="Finished after the pause.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Research it")
    await until(lambda: SCOUT in svc.busy and LEAD not in svc.busy)
    result = await rt.engage_kill_switch()
    assert result["team_goals_paused"] == 1
    paused = await _goal(rt, goal.goal_id)
    assert paused.state is TeamGoalState.PAUSED and paused.note == "society_paused"
    with pytest.raises(TeamLeadError, match="paused"):
        await leads.start_goal("team", "Another")
    with pytest.raises(TeamLeadError, match="paused"):
        await leads.resume(goal.goal_id)

    # The real switch stops every society chat; the stopped task is on the board.
    await svc.cancel(SCOUT)
    await until(lambda: not rt.scheduler.running)
    assert len(seen) == 1  # a paused goal does not wake its lead

    await rt.release_kill_switch()
    await leads.resume(goal.goal_id)
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert "[team goal resumed for team Launch]" in seen[1]
    assert '"Research": blocked' in seen[1]


async def test_the_turn_budget_pauses_a_goal_until_the_person_resumes(team):
    rt, svc = team
    leads = rt.team_leads
    goal = await leads.start_goal("team", "Endless")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    await rt.store.update_team_goal(
        goal.goal_id, {"state": "running", "lead_turns": MAX_LEAD_TURNS, "done_ms": None}
    )
    leads._resumed.add(goal.goal_id)  # noqa: SLF001 — something to say, so the budget is what stops it
    await leads._lead_turn(goal.goal_id)  # noqa: SLF001
    parked = await _goal(rt, goal.goal_id)
    assert parked.state is TeamGoalState.PAUSED and parked.note == "turn_budget"
    await leads.resume(goal.goal_id)
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))


async def test_a_failed_lead_turn_pauses_the_goal_with_the_reason(team):
    rt, svc = team
    svc.failing.add(LEAD)
    goal = await rt.team_leads.start_goal("team", "Do it")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.PAUSED))
    assert (await _goal(rt, goal.goal_id)).note == "error:provider unavailable"


async def test_a_running_goal_comes_back_paused_after_a_restart(tmp_path: Path):
    svc = TeamChatFake(AgentChatStore(tmp_path / "chat.db"))
    rt = _runtime(tmp_path, svc)
    await rt.ensure_started()
    for name in ("Boss", "Scout"):
        await rt.roster.create(name=name, provider="ollama", model="fake")
    await rt.store.create_chat_group("team", "Launch", ["boss", "scout"])
    await rt.team_leads.set_lead("team", "boss")
    svc.gates[SCOUT] = asyncio.Event()

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        await rt.team_leads.assign("boss", member="scout", title="Research", brief="Go.")
        return "Assigned."

    svc.scripts[LEAD] = boss
    goal = await rt.team_leads.start_goal("team", "Research it")
    await until(lambda: SCOUT in svc.busy and LEAD not in svc.busy)
    await rt.close()  # the app closes mid-task; the member's turn dies with it
    await svc.close()

    again = _runtime(tmp_path, svc)
    await again.ensure_started()
    try:
        back = await again.team_leads.get_goal(goal.goal_id)
        assert back.state is TeamGoalState.PAUSED and back.note == "interrupted"
        task = (await again.team_leads.tasks(goal.goal_id))[0]
        assert task.state is TeamTaskState.BLOCKED and "Interrupted" in task.result
        assert not task.reviewed
        assert again.team_leads.group_led_by("boss") == "team"
        # Reading the board resumes nothing.
        await again.team_leads.snapshot("team")
        assert len(svc.prompts(LEAD)) == 1
    finally:
        await again.close()
        svc.store.close()


async def test_the_sweep_reports_a_run_that_died_silently(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[SCOUT] = asyncio.Event()
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if len(seen) == 1:
            await leads.assign("boss", member="scout", title="Research", brief="Go.")
            return "Assigned."
        await leads.report("boss", status="failed", summary="Scout went silent.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Research it")
    await until(lambda: SCOUT in svc.busy and LEAD not in svc.busy)
    task = (await leads.tasks(goal.goal_id))[0]
    await leads.sweep()  # fresh work is left alone
    assert (await leads.tasks(goal.goal_id))[0].state is TeamTaskState.RUNNING
    # The run vanished from the scheduler and the task shows no sign of life.
    rt.scheduler.note_run_ended(task.run_id)
    await rt.store.update_team_task(task.task_id, {"updated_ms": 1})
    await leads.sweep()
    # The member's chat still runs that turn: it is not dead, so nobody is told so.
    assert (await leads.tasks(goal.goal_id))[0].state is TeamTaskState.RUNNING
    assert len(seen) == 1
    # Now the turn is gone without an ending in the chat: that is a silent death.
    svc.busy.discard(SCOUT)
    await leads.sweep()
    await until(_state_is(rt, goal.goal_id, TeamGoalState.FAILED))
    assert "The run ended without a result." in seen[1]


async def test_a_closed_goal_starts_nothing_more_from_its_open_turn(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[LEAD] = asyncio.Event()
    goal = await leads.start_goal("team", "Research it")
    await until(lambda: LEAD in svc.busy)
    # The goal closes while its lead's turn is still open (the lead reported,
    # or the person stopped it and the turn has not ended yet).
    await rt.store.update_team_goal(goal.goal_id, {"state": "cancelled"})
    with pytest.raises(TeamLeadError, match="no longer in progress"):
        await leads.assign("boss", member="scout", title="Research", brief="Go.")
    with pytest.raises(TeamLeadError, match="no longer in progress"):
        await leads.report("boss", status="done", summary="Too late.")
    assert await leads.tasks(goal.goal_id) == []
    assert [g.goal_id for g in await leads.goals("team")] == [goal.goal_id]
    assert svc.prompts(SCOUT) == []
    with pytest.raises(TeamLeadError, match="not in progress"):
        await leads.tell(goal.goal_id, "Still there?")
    with pytest.raises(TeamLeadError, match="not paused"):
        await leads.resume(goal.goal_id)
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.CANCELLED
    svc.gates[LEAD].set()
    await until(svc.idle)


async def test_a_stop_wins_over_a_stale_pause_or_resume(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[LEAD] = asyncio.Event()
    goal = await leads.start_goal("team", "Do it")
    await until(lambda: LEAD in svc.busy)
    stale = await _goal(rt, goal.goal_id)  # read while the goal was running
    await leads.cancel(goal.goal_id)
    await leads._park(stale, "error:late")  # noqa: SLF001 — a late pause from the stale read
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.CANCELLED
    assert not await leads._finish(stale, TeamGoalState.DONE, report="late")  # noqa: SLF001
    assert (await _goal(rt, goal.goal_id)).report == ""


async def test_a_resume_during_a_turn_that_is_winding_down_is_not_lost(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[LEAD] = asyncio.Event()
    goal = await leads.start_goal("team", "Do it")
    await until(lambda: LEAD in svc.busy)
    await rt.engage_kill_switch()
    await rt.release_kill_switch()
    # The person continues before the stopped turn has fully ended.
    await leads.resume(goal.goal_id)
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.RUNNING
    await svc.cancel(LEAD)  # the stopped turn ends "cancelled"
    svc.gates.pop(LEAD)
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert any(text.startswith("[team goal resumed") for text in svc.prompts(LEAD))


async def _lost_assignment(rt, svc):
    """A task whose start the scheduler abandoned: on the board, never claimed."""
    from dodaios.agent_chat.events import make_event
    from dodaios.society.chat_binding import ensure_session

    leads = rt.team_leads
    rt.scheduler.detach()  # the ASSIGN gets neither a CLAIM nor a VETO
    receipt = await leads.assign("boss", member="scout", title="Research", brief="Go.")
    rt.scheduler.attach()
    task = (await leads.tasks(receipt["goal_id"]))[0]
    assert task.state is TeamTaskState.ASSIGNED
    await rt.store.update_team_task(task.task_id, {"updated_ms": 1})
    scout = await rt.roster.get("scout")
    ensure_session(svc, rt.config(), scout)

    def say(kind: str, payload: dict[str, Any]) -> None:
        svc.store.append_event(SCOUT, make_event(kind, payload))

    return leads, receipt["goal_id"], task, say


async def test_a_lost_assignment_is_read_back_from_the_members_chat(team):
    rt, svc = team
    leads, goal_id, task, say = await _lost_assignment(rt, svc)
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        await leads.report("boss", status="done", summary="Scout found it.")
        return "Closed."

    svc.scripts[LEAD] = boss
    # The member works on it all the same: its chat holds the framed assignment.
    framed = f"[assignment from boss] Go. Message id: {task.assign_event_id}; sender id: boss"
    say("user_message", {"text": framed})
    svc.busy.add(SCOUT)
    await leads.sweep()
    # Still running: the task is left alone and the lead is not woken.
    assert (await leads.tasks(goal_id))[0].state is TeamTaskState.ASSIGNED
    assert seen == []

    say("assistant_text", {"turn_id": "t1", "text": "Found: three sources."})
    say("turn_finished", {"turn_id": "t1", "status": "done"})
    svc.busy.discard(SCOUT)
    await leads.sweep()
    await until(_state_is(rt, goal_id, TeamGoalState.DONE))
    done = (await leads.tasks(goal_id))[0]
    assert done.state is TeamTaskState.DONE and done.result == "Found: three sources."
    # The lead heard the real result once; nothing was handed out twice.
    assert len(seen) == 1 and "Found: three sources." in seen[0]
    assert len(await leads.tasks(goal_id)) == 1 and svc.prompts(SCOUT) == []


async def test_the_slow_start_drill_loses_the_start_and_the_sweep_recovers_it(
    team, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
    """The drill file makes a real start outlast the board's patience."""
    from dodaios.society import bus

    rt, svc = team
    leads = rt.team_leads
    monkeypatch.setattr(bus, "_HANDLER_TIMEOUT_S", 0.05)
    (tmp_path / "society_drill.json").write_text('{"slow_start_s": 0.4}', encoding="utf-8")
    svc.replies[SCOUT] = "Found: three sources."
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if len(seen) == 1:
            receipt = await leads.assign("boss", member="scout", title="Research", brief="Go.")
            assert receipt["state"] == "assigned"  # the start was never confirmed
            return "Assigned."
        await leads.report("boss", status="done", summary="Scout found it.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Research it")
    await until(lambda: len(svc.prompts(SCOUT)) == 1 and svc.idle())
    task = (await leads.tasks(goal.goal_id))[0]
    # The member worked, yet the board holds no claim and no result for it.
    assert task.state is TeamTaskState.ASSIGNED and task.run_id == ""
    kinds = [e.msg_type for e in await rt.store.events_for_trace(task.trace_id)]
    assert kinds == [MsgType.ASSIGN]
    assert not rt.scheduler.running and len(seen) == 1

    await rt.store.update_team_task(task.task_id, {"updated_ms": 1})
    await leads.sweep()
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    done = (await leads.tasks(goal.goal_id))[0]
    assert done.state is TeamTaskState.DONE and done.result == "Found: three sources."
    assert len(await leads.tasks(goal.goal_id)) == 1 and len(svc.prompts(SCOUT)) == 1

    # Without the file the same start is claimed as usual.
    (tmp_path / "society_drill.json").unlink()
    assert rt._drill_seconds("slow_start_s") == 0.0  # noqa: SLF001
    (tmp_path / "society_drill.json").write_text("not json", encoding="utf-8")
    assert rt._drill_seconds("slow_start_s") == 0.0  # noqa: SLF001
    (tmp_path / "society_drill.json").write_text('{"slow_start_s": 900}', encoding="utf-8")
    assert rt._drill_seconds("slow_start_s") == 30.0  # noqa: SLF001


async def test_a_lost_assignment_that_failed_or_never_arrived_is_reported_blocked(team):
    rt, svc = team
    leads, goal_id, task, say = await _lost_assignment(rt, svc)
    svc.replies[LEAD] = "Noted."
    await leads.sweep()  # the member's chat never saw it
    first = (await leads.tasks(goal_id))[0]
    assert first.state is TeamTaskState.BLOCKED and "never picked up" in first.result

    await rt.store.update_team_task(
        task.task_id, {"state": "assigned", "updated_ms": 1, "result": "", "done_ms": None}
    )
    say("user_message", {"text": f"Message id: {task.assign_event_id}; sender id: boss"})
    say("turn_finished", {"turn_id": "t1", "status": "error", "error": "provider unavailable"})
    await leads.sweep()
    failed = (await leads.tasks(goal_id))[0]
    assert failed.state is TeamTaskState.BLOCKED and failed.result == "provider unavailable"


# ------------------------------------------------- assignments from the board


async def test_work_handed_to_a_lead_becomes_a_goal_and_answers_its_origin(team):
    rt, svc = team
    leads = rt.team_leads

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        if text.startswith("[team goal for"):
            await leads.assign("boss", member="scout", title="Research", brief="Go.")
            return "Assigned."
        await leads.report("boss", status="done", summary="Researched and summarized.")
        return "Closed."

    svc.scripts[LEAD] = boss
    env = await rt.store.append_and_publish(
        SocietyEnvelope(
            msg_type=MsgType.ASSIGN,
            from_agent="jarvis",
            to_agent="boss",
            trace_id="voice:abc",
            payload={"text": "Research the market", "reply_policy": "always", "lang": "de"},
        )
    )
    events = await rt.store.events_for_trace("voice:abc")
    claim = next(e for e in events if e.msg_type is MsgType.CLAIM)
    run_id = claim.payload["run_id"]
    # A goal may wait for days; it never holds one of the lead's run slots.
    assert run_id.startswith("goal:") and run_id not in rt.scheduler.running
    goal_id = run_id.removeprefix("goal:")
    assert (await _goal(rt, goal_id)).lang == "de"

    await until(_state_is(rt, goal_id, TeamGoalState.DONE))

    async def answered() -> list[SocietyEnvelope]:
        events = await rt.store.events_for_trace("voice:abc")
        return [e for e in events if e.msg_type is MsgType.RESULT]

    await until(answered)
    (result,) = await answered()
    assert result.parent_event_id == env.event_id and result.from_agent == "boss"
    assert result.payload["status"] == "done"
    assert result.payload["done"] == "Researched and summarized."
    assert not rt.scheduler.running
    # The lead's own chat never ran the raw assignment as a second turn.
    assert all(not text.startswith("[assignment from") for text in svc.prompts(LEAD))


async def test_a_lead_assigning_from_its_own_chat_opens_a_goal(team):
    rt, svc = team
    leads = rt.team_leads
    receipt = await leads.assign("boss", member="scout", title="Check the logs", brief="Go.")
    goal = await _goal(rt, receipt["goal_id"])
    assert goal.title == "Check the logs" and goal.origin == {"kind": "lead"}
    svc.replies[LEAD] = "Logs are clean."
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert "[team update for team Launch]" in svc.prompts(LEAD)[0]


# ------------------------------------------------- the person's own chat turn


@contextmanager
def person_says(text: str, session_id: str = LEAD, *, direct_user: bool = True):
    """Run the body as a tool call inside a turn of the lead's chat."""
    token = current_chat_turn.set(
        ChatTurn(
            session_id=session_id,
            turn_id="person-turn",
            user_text=text,
            direct_user=direct_user,
            trace_id="trace",
        )
    )
    try:
        yield
    finally:
        current_chat_turn.reset(token)


async def test_work_asked_for_in_the_leads_chat_opens_a_goal_in_the_persons_words(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[SCOUT] = asyncio.Event()
    asked = "Find out why signups dropped.\nLook at last week first.\n\n[tools: browser]"
    with person_says(asked):
        receipt = await TeamAssignTool(rt, "boss").execute(
            {"member": "Scout", "title": "Pull the numbers", "task": "Go.", "goal": "Signup drop"},
            None,
        )
    assert receipt.success
    goal = await _goal(rt, receipt.output["goal_id"])
    assert goal.origin == {"kind": "chat"} and goal.title == "Signup drop"
    assert goal.text == "Find out why signups dropped.\nLook at last week first."
    assert (await leads.snapshot("team"))["goals"][0]["source"] == "chat"

    # Without a name from the lead the goal is called by the person's first line.
    await leads.cancel(goal.goal_id)
    with person_says("Write the launch post"):
        again = await leads.assign("boss", member="writer", title="Draft", brief="Go.")
    assert (await _goal(rt, again["goal_id"])).title == "Write the launch post"
    svc.gates[SCOUT].set()


async def test_an_answer_in_the_leads_chat_continues_a_goal_that_waits(team):
    rt, svc = team
    leads = rt.team_leads

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        await leads.report("boss", status="needs_input", summary="Which market?")
        return "I need one answer."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Plan the launch")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.NEEDS_INPUT))
    await until(lambda: svc.idle() and not leads._turns)  # noqa: SLF001 — the turn is read back

    # A turn nobody typed (a check-in, another agent's message) cannot answer for the person.
    for stranger in (
        person_says("Germany", direct_user=False),
        person_says("Germany", session_id=SCOUT),
    ):
        with stranger, pytest.raises(TeamLeadError, match="answers you in this chat"):
            await leads.assign("boss", member="scout", title="Research", brief="Go.")
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.NEEDS_INPUT

    svc.gates[SCOUT] = asyncio.Event()
    with person_says("Germany"):
        receipt = await leads.assign("boss", member="scout", title="Research", brief="Go.")
    assert receipt["goal_id"] == goal.goal_id
    resumed = await _goal(rt, goal.goal_id)
    assert resumed.state is TeamGoalState.RUNNING and resumed.lead_turns == 0
    svc.scripts.pop(LEAD)
    svc.gates[SCOUT].set()
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))


async def test_a_go_ahead_in_the_leads_chat_continues_a_paused_goal_but_not_past_the_switch(team):
    rt, svc = team
    leads = rt.team_leads
    svc.failing.add(LEAD)
    goal = await leads.start_goal("team", "Plan the launch")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.PAUSED))
    svc.failing.discard(LEAD)

    await rt.store.set_kill_switch(True)
    with person_says("Go on"), pytest.raises(TeamLeadError):
        await leads.report("boss", status="progress", summary="Back on it.")
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.PAUSED
    await rt.store.set_kill_switch(False)

    with person_says("Go on"):
        await leads.report("boss", status="done", summary="Finished after all.")
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.DONE


async def test_tasks_handed_out_together_from_the_chat_share_one_goal(team):
    rt, svc = team
    leads = rt.team_leads
    svc.gates[SCOUT] = asyncio.Event()
    svc.gates[WRITER] = asyncio.Event()
    with person_says("Research it and write it up"):
        first, second = await asyncio.gather(
            leads.assign("boss", member="scout", title="Research", brief="Go."),
            leads.assign("boss", member="writer", title="Write", brief="Go."),
        )
    assert first["goal_id"] == second["goal_id"]
    goals = await leads.goals("team")
    assert len(goals) == 1 and len(await leads.tasks(goals[0].goal_id)) == 2
    svc.gates[SCOUT].set()
    svc.gates[WRITER].set()


async def test_a_stop_wins_over_an_answer_that_is_on_its_way(team):
    rt, svc = team
    leads = rt.team_leads

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        await leads.report("boss", status="needs_input", summary="Which market?")
        return "I need one answer."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Plan the launch")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.NEEDS_INPUT))
    await until(lambda: svc.idle() and not leads._turns)  # noqa: SLF001 — the turn is read back

    # The person stops the goal while the lead's call is between its checks.
    resolved = leads._goal_for_lead  # noqa: SLF001 — the seam between resolving and assigning

    async def stopped_meanwhile(lead_id: str, **kwargs: Any) -> Any:
        found = await resolved(lead_id, **kwargs)
        await leads.cancel(found.goal_id)
        return found

    leads._goal_for_lead = stopped_meanwhile  # type: ignore[method-assign]  # noqa: SLF001
    with person_says("Germany"), pytest.raises(TeamLeadError, match="no longer in progress"):
        await leads.assign("boss", member="scout", title="Research", brief="Go.")
    assert (await _goal(rt, goal.goal_id)).state is TeamGoalState.CANCELLED
    assert await leads.tasks(goal.goal_id) == []


async def test_the_persons_turn_is_followed_up_like_a_managing_turn(team):
    rt, svc = team
    leads = rt.team_leads
    seen: list[str] = []

    async def boss(text: str, kwargs: dict[str, Any]) -> str:
        seen.append(text)
        if len(seen) == 1:
            await leads.report("boss", status="needs_input", summary="Which market?")
            return "I need one answer."
        await leads.report("boss", status="done", summary="Germany it is.")
        return "Closed."

    svc.scripts[LEAD] = boss
    goal = await leads.start_goal("team", "Plan the launch")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.NEEDS_INPUT))
    await until(lambda: svc.idle() and not leads._turns)  # noqa: SLF001 — the turn is read back

    # The lead answers the person in plain words and reports nothing: the
    # goal it reopened is asked once for a typed report.
    with person_says("Germany"):
        await leads.report("boss", status="progress", summary="Noted: Germany.")
    await leads.person_turn_ended("boss", ok=True, final="Noted.")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    assert "[team goal check for team Launch]" in seen[1]

    # A turn that touched no goal, or failed, starts nothing.
    sent = len(svc.sent)
    await leads.person_turn_ended("boss", ok=True, final="Hello.")
    await leads.person_turn_ended("scout", ok=True, final="Hello.")
    await asyncio.sleep(0.05)
    assert len(svc.sent) == sent


async def test_the_runtime_reports_the_persons_turn_in_a_leads_chat(team):
    rt, _ = team
    ended: list[tuple[str, bool, str]] = []

    async def record(lead_id: str, *, ok: bool, final: str = "") -> None:
        ended.append((lead_id, ok, final))

    rt.team_leads.person_turn_ended = record  # type: ignore[method-assign]

    def completion(session_id: str, direct_user: bool, status: str) -> Any:
        events = [
            {"kind": "user_message", "payload": {"text": "Hi"}},
            {"kind": "assistant_text", "payload": {"text": "On it."}},
            {"kind": "turn_finished", "payload": {"status": status}},
        ]
        turn = ChatTurn(
            session_id=session_id, turn_id="t", user_text="Hi", direct_user=direct_user,
            trace_id="x",
        )
        return SimpleNamespace(turn=turn, events_json=json.dumps(events))

    for session_id, direct_user, status in (
        (LEAD, True, "error"),
        (LEAD, False, "done"),
        (SCOUT, True, "done"),
    ):
        await rt.turn_completed(
            SimpleNamespace(session_id=session_id), completion(session_id, direct_user, status)
        )
    await until(lambda: ended)
    assert ended == [("boss", False, "On it.")]


async def test_the_board_names_what_was_finished_so_it_is_not_done_twice(team):
    rt, svc = team
    leads = rt.team_leads
    svc.replies[LEAD] = "Shipped the page."
    goal = await leads.start_goal("team", "Launch the page")
    await until(_state_is(rt, goal.goal_id, TeamGoalState.DONE))
    board = await leads.board("boss")
    assert board["goals"] == []
    assert board["finished"] == [
        {"title": "Launch the page", "state": "done", "report": "Shipped the page."}
    ]


# ---------------------------------------------------------------- the tools


async def test_only_a_lead_holds_the_team_tools(team):
    rt, _ = team
    assert set(team_lead_tools(rt, "boss")) == {
        TEAM_ASSIGN_TOOL_NAME,
        TEAM_BOARD_TOOL_NAME,
        TEAM_REPORT_TOOL_NAME,
    }
    assert team_lead_tools(rt, "scout") == {}

    from dodaios.society.surface import society_tools

    lead_session = SimpleNamespace(session_id=LEAD, cwd="", permission_mode="")
    member_session = SimpleNamespace(session_id=SCOUT, cwd="", permission_mode="")
    cfg = rt.config()
    assert TEAM_ASSIGN_TOOL_NAME in society_tools(cfg, None, lead_session)
    assert TEAM_ASSIGN_TOOL_NAME not in society_tools(cfg, None, member_session)


async def test_the_assign_tool_composes_a_brief_and_refuses_strangers(team):
    rt, svc = team
    svc.gates[SCOUT] = asyncio.Event()
    tool = TeamAssignTool(rt, "boss")
    await rt.roster.create(name="Outsider", provider="ollama", model="fake")
    refused = await tool.execute({"member": "Outsider", "title": "X", "task": "Y"}, None)
    assert not refused.success and "not a member of your team" in refused.error
    assert "Scout" in refused.error and "Boss" not in refused.error
    own = await tool.execute({"member": "Boss", "title": "X", "task": "Y"}, None)
    assert not own.success

    ok = await tool.execute(
        {
            "member": "Scout",
            "title": "Research",
            "task": "Find three competitors.",
            "context": "We sell bikes.",
            "completion_criteria": "A list with links.",
        },
        None,
    )
    assert ok.success and ok.output["state"] == "running" and ok.output["member"] == "Scout"
    task = (await rt.team_leads.tasks(ok.output["goal_id"]))[0]
    assert "Relevant context:\nWe sell bikes." in task.brief
    assert "Completion criteria:\nA list with links." in task.brief
    prompt = svc.prompts(SCOUT)[0]
    assert "Research\n\nFind three competitors." in prompt
    svc.gates[SCOUT].set()

    report = TeamReportTool(rt, "boss")
    bad = await report.execute({"status": "maybe", "summary": "x"}, None)
    assert not bad.success
    update = await report.execute({"status": "progress", "summary": "Half way."}, None)
    assert update.success and update.output["recorded"] == "progress"
    assert "do not repeat the report" in update.output["next"]
    stranger = await TeamReportTool(rt, "scout").execute(
        {"status": "done", "summary": "x"}, None
    )
    assert not stranger.success and "do not lead" in stranger.error


async def test_briefings_tell_the_lead_and_its_members_about_the_team(team):
    rt, _ = team
    await rt.team_leads.set_mission("team", "Ship the launch.")
    lead = rt.team_leads.briefing("boss")
    assert '## You lead the team "Launch"' in lead
    assert "Members you manage: Scout; Writer." in lead
    assert "Team mission: Ship the launch." in lead and "society_team_assign" in lead
    member = rt.team_leads.briefing("scout")
    assert 'member of the team "Launch", led by Boss' in member
    assert rt.team_leads.briefing("jarvis") == ""
    # Stable between team edits: the provider prompt cache holds.
    assert rt.team_leads.briefing("boss") == lead


async def test_jarvis_card_names_the_teams_that_have_a_lead(team):
    rt, _ = team
    card = render_lead_card(
        rt.roster.snapshot(), rt.catalog(), teams=rt.team_leads.teams_snapshot()
    )
    assert "- Launch — lead: Boss · members: Scout, Writer" in card
    assert "goes to its lead with delegate_to_agent" in card
    assert "Teams with their own lead" not in render_lead_card(rt.roster.snapshot(), rt.catalog())


# ------------------------------------------------------- check-ins, storage


async def test_a_check_in_is_an_agent_routine_the_person_switches_on(team):
    rt, _ = team
    fake = FakeTasks()
    rt.task_services = lambda: (fake, fake)
    leads = rt.team_leads
    assert (await leads.snapshot("team"))["checkin_minutes"] == 0
    with pytest.raises(TeamLeadError):
        await leads.set_checkin("team", 7)

    assert await leads.set_checkin("team", 60) == 60
    (spec,) = fake.specs.values()
    assert "team-checkin:team" in spec.tags and "agent:boss" in spec.tags
    assert spec.trigger.interval_seconds == 3600
    assert "society_team_board" in spec.action.prompt
    assert (await leads.snapshot("team"))["checkin_minutes"] == 60

    await leads.set_checkin("team", 240)
    assert len(fake.specs) == 1 and len(fake.cancelled) == 1
    await leads.set_lead("team", None)
    assert fake.specs == {}


async def test_a_check_in_with_initiative_may_set_the_next_goal(team):
    rt, _ = team
    fake = FakeTasks()
    rt.task_services = lambda: (fake, fake)
    leads = rt.team_leads
    assert (await leads.snapshot("team"))["initiative"] is False

    await leads.set_checkin("team", 60)
    assert await leads.checkin("team") == (60, False)
    assert (await leads.snapshot("team"))["initiative"] is False

    await leads.set_checkin("team", 240, initiative=True)
    (spec,) = fake.specs.values()
    assert {"team-checkin:team", "team-initiative:team"} <= set(spec.tags)
    assert INITIATIVE_PROMPT in spec.action.prompt and "take the initiative" in INITIATIVE_PROMPT
    snapshot = await leads.snapshot("team")
    assert snapshot["checkin_minutes"] == 240 and snapshot["initiative"] is True

    # A new lead inherits how the team is watched.
    await leads.set_lead("team", "writer")
    (moved,) = fake.specs.values()
    assert "agent:writer" in moved.tags and "team-initiative:team" in moved.tags
    await leads.set_checkin("team", 0, initiative=True)
    assert fake.specs == {} and await leads.checkin("team") == (0, False)


async def test_an_older_database_gains_the_lead_columns(tmp_path: Path):
    path = tmp_path / "society.db"
    conn = await aiosqlite.connect(path)
    await conn.execute(
        "CREATE TABLE society_chat_groups (group_id TEXT PRIMARY KEY, name TEXT NOT NULL, "
        "members_json TEXT NOT NULL, created_ms INTEGER NOT NULL, updated_ms INTEGER NOT NULL)"
    )
    await conn.execute(
        "INSERT INTO society_chat_groups VALUES (?, ?, ?, 1, 1)", ("old", "Old team", '["a", "b"]')
    )
    await conn.commit()
    await conn.close()

    store = SocietyStore(path)
    await store.open()
    try:
        group = await store.get_chat_group("old")
        assert group["members"] == ["a", "b"]
        assert group["lead_agent_id"] is None and group["mission"] == ""
        assert (await store.set_chat_group_lead("old", "a"))["lead_agent_id"] == "a"
        created = await store.create_chat_group("new", "New", ["a", "b"])
        assert created["lead_agent_id"] is None
    finally:
        await store.close()


async def test_deleting_a_team_stops_its_goals_and_frees_its_lead(team):
    rt, svc = team
    svc.gates[LEAD] = asyncio.Event()
    goal = await rt.team_leads.start_goal("team", "Do it")
    await until(lambda: LEAD in svc.busy)
    await rt.team_leads.group_deleted("team")
    await rt.store.delete_chat_group("team")
    await rt.team_leads.refresh()
    assert await rt.team_leads.get_goal(goal.goal_id) is None
    assert (await rt.roster.get("boss")).tier is Tier.SPECIALIST
    assert rt.team_leads.group_led_by("boss") is None
    assert (LEAD, svc.sent[0][3]) in svc.cancelled