Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

28.7 KB
Raw Blame
"""``ghostwrite_email``: the handler's fences and the loop's offer
(orchestrator/delegation_tools.py, orchestrator/executive.py)."""
from __future__ import annotations

import asyncio
import json
from collections.abc import Iterator
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from unittest.mock import patch

import pytest

from openexecutive.audit import logger as audit_logger
from openexecutive.audit.logger import AuditLogger, log_event
from openexecutive.delegation import caps
from openexecutive.delegation import drafts as ddrafts
from openexecutive.delegation import ghostwriter as gw
from openexecutive.delegation import settings as dsettings
from openexecutive.delegation.gmail import (
    CreatedDraft,
    DraftSpec,
    GmailError,
    MailMessage,
    MailThread,
    ThreadSummary,
)
from openexecutive.delegation.settings import (
    DelegationOverride,
    TurnDelegation,
    pin_turn_delegation,
)
from openexecutive.memory import episodic
from openexecutive.orchestrator import delegation_tools as dt
from openexecutive.orchestrator.schedule_tools import current_session, set_session
from openexecutive.orchestrator.session import Session
from openexecutive.people import registry as people_registry
from openexecutive.people import store as people_store
from openexecutive.people.models import Person

from ._agent_loop_fakes import FinalMsg, ScriptedProvider, TextBlock, ToolUseBlock

OWNER = "[email protected]"
TEAM = "[email protected]"
DANA = "[email protected]"


@pytest.fixture(autouse=True)
def db(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
    path = tmp_path / "episodic.db"
    monkeypatch.setattr(episodic, "DB_PATH", path)
    monkeypatch.setattr(people_store, "DB_PATH", path)
    episodic.initialize_db(path)
    people_store.initialize_db(path)
    people_registry.invalidate()
    monkeypatch.setattr(audit_logger, "_default_logger", AuditLogger(db_path=path))
    prior = current_session.get()
    current_session.set(None)
    yield path
    current_session.set(prior)
    people_registry.invalidate()


@pytest.fixture(autouse=True)
def fresh_turn_state() -> Iterator[None]:
    """No pin or draft count carries over from another test (both live at
    module level: the turn's context variable and the per-person counters)."""
    token = dsettings._TURN.set(None)
    caps._SAVED_TODAY.clear()
    caps._IN_FLIGHT.clear()
    yield
    dsettings._TURN.reset(token)
    caps._SAVED_TODAY.clear()
    caps._IN_FLIGHT.clear()


@pytest.fixture
def roster() -> SimpleNamespace:
    principal = people_store.upsert_person(full_name="Olivia Owner", is_principal=True, email=OWNER)
    teammate = people_store.upsert_person(full_name="Ben Teammate", role="Ops", email=TEAM)
    people_registry.invalidate()
    return SimpleNamespace(principal=principal, teammate=teammate)


def _msg(i: int, sender: str, text: str, *, labels: list[str] | None = None, **kw: Any) -> MailMessage:
    return MailMessage(
        id=f"m{i}", thread_id="t1", from_addr=sender, from_name=sender.split("@")[0].title(),
        to=[OWNER], subject="Brand refresh pilot", date=f"Mon {i}",
        message_id_header=f"<m{i}@mail.example>", labels=labels or ["INBOX"], text=text, **kw,
    )


class FakeMailbox:
    def __init__(self, *, opened: str = OWNER, search: list[ThreadSummary] | None = None) -> None:
        self.opened = opened
        self.search = search if search is not None else []
        self.threads: dict[str, MailThread] = {
            "t1": MailThread(id="t1", messages=[
                _msg(1, DANA, "Can we start the pilot Oct 5?", reply_to="[email protected]",
                     cc=["[email protected]"]),
            ]),
        }
        self.drafts: list[DraftSpec] = []

    async def profile_email(self) -> str:
        return self.opened

    async def search_threads(self, query: str, *, max_results: int = 5) -> list[ThreadSummary]:
        return self.search

    async def get_thread(self, thread_id: str) -> MailThread:
        return self.threads[thread_id]

    async def create_draft(self, spec: DraftSpec) -> CreatedDraft:
        self.drafts.append(spec)
        return CreatedDraft(draft_id="d1", message_id="m99", thread_id=spec.thread_id or "tnew")


def _owner() -> Person:
    person = people_store.find_principal_person()
    assert person is not None
    return person


def _session(mailbox: FakeMailbox, speaker_text: str = "reply to Dana as me") -> Session:
    session = Session(delegation_override=DelegationOverride(enabled=True, gmail=mailbox, person=_owner()))
    assert pin_turn_delegation(session, speaker_text).offered
    return session


@pytest.fixture
def composer(monkeypatch: pytest.MonkeyPatch) -> list[str]:
    turns: list[str] = []

    async def fake(model: str, system: str, turn: str) -> dict[str, Any]:
        turns.append(turn)
        return {"subject": "Hello", "body": "Hi Dana,\n\nYes to Oct 5.\n\nBest,\nOlivia", "open_questions": []}

    monkeypatch.setattr(gw, "_call_model", fake)
    return turns


def _run(session: Session | None, tool_input: dict[str, Any]) -> dict[str, Any]:
    async def go() -> str:
        with set_session(session):
            return await dt.handle_ghostwrite_email(tool_input)

    return json.loads(asyncio.run(go()))


# --------------------------------------------------------------------------- #
# Fences
# --------------------------------------------------------------------------- #


def test_refused_on_a_turn_it_was_not_offered_to(roster: SimpleNamespace) -> None:
    assert "not available" in _run(None, {"intent": "x"})["error"]
    unpinned = Session(caller_person_id=roster.principal)
    assert "not available" in _run(unpinned, {"intent": "x"})["error"]
    off = Session(turn_delegation=TurnDelegation(offered=False, person_id=roster.principal))
    assert "not available" in _run(off, {"intent": "x"})["error"]


def test_a_stale_pin_on_an_unverified_surface_is_refused(roster: SimpleNamespace) -> None:
    # Pinned as offered, but the session is not the principal on a verified
    # surface (e.g. reused by another caller): the handler checks again.
    session = Session(turn_delegation=TurnDelegation(offered=True, person_id=roster.principal))
    assert "not available" in _run(session, {"intent": "x", "to": [TEAM]})["error"]


def test_an_intent_is_required(roster: SimpleNamespace) -> None:
    assert "intent" in _run(_session(FakeMailbox()), {"thread_id": "t1"})["error"]


def test_the_per_turn_and_daily_caps(roster: SimpleNamespace, monkeypatch: pytest.MonkeyPatch) -> None:
    session = _session(FakeMailbox())
    session.turn_delegation.drafts = dt.DRAFTS_PER_TURN
    assert "drafts this turn" in _run(session, {"intent": "x", "thread_id": "t1"})["error"]
    monkeypatch.setattr(caps, "drafts_today", lambda _pid: 10_000)
    assert "limit" in _run(_session(FakeMailbox()), {"intent": "x", "thread_id": "t1"})["error"]


def test_the_daily_cap_fails_closed(roster: SimpleNamespace, monkeypatch: pytest.MonkeyPatch) -> None:
    def broken(*_a: Any, **_kw: Any) -> Any:
        raise RuntimeError("db locked")

    monkeypatch.setattr(ddrafts, "count_since", broken)
    assert caps.drafts_today(roster.principal) is None
    session = _session(FakeMailbox())
    assert "draft limit" in _run(session, {"intent": "x", "thread_id": "t1"})["error"]
    assert session.turn_delegation.touched_mail is False


def test_the_daily_cap_setting_stays_bounded() -> None:
    from openexecutive.config import Settings

    bounds = [m for m in Settings.model_fields["delegation_max_drafts_per_day"].metadata if hasattr(m, "le")]
    assert [b.le for b in bounds] == [1000]


def test_a_mailbox_that_is_not_theirs_is_refused_and_the_turn_is_marked(roster: SimpleNamespace) -> None:
    session = _session(FakeMailbox(opened="[email protected]"))
    result = _run(session, {"intent": "x", "thread_id": "t1"})
    assert result["status"] == "mismatch"
    assert session.turn_delegation.touched_mail is True


# --------------------------------------------------------------------------- #
# Replies
# --------------------------------------------------------------------------- #


def test_several_matches_ask_which_one_and_draft_nothing(roster: SimpleNamespace) -> None:
    mailbox = FakeMailbox(search=[
        ThreadSummary(id="t1", subject="Pilot", sender="Dana <dana@x>", date="Mon"),
        ThreadSummary(id="t2", subject="Pilot II\n<script>", sender="Dana", date="Tue"),
    ])
    result = _run(_session(mailbox), {"intent": "yes", "find": "from:dana pilot"})
    assert result["status"] == "choose"
    assert [c["thread_id"] for c in result["candidates"]] == ["t1", "t2"]
    assert "\n" not in result["candidates"][1]["subject"]
    assert mailbox.drafts == []


def test_no_match_is_not_found(roster: SimpleNamespace) -> None:
    assert _run(_session(FakeMailbox()), {"intent": "yes", "find": "nothing"})["status"] == "not_found"


def test_a_reply_goes_to_the_sender_never_their_reply_to(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    mailbox = FakeMailbox()
    result = _run(_session(mailbox), {"intent": "Yes to Oct 5.", "thread_id": "t1"})
    assert result["status"] == "drafted"
    spec = mailbox.drafts[0]
    assert (spec.to, spec.cc) == ([DANA], [])
    assert spec.thread_id == "t1"
    assert spec.subject == "Re: Brand refresh pilot"
    assert spec.in_reply_to == "<[email protected]>"
    assert "reply_to_ignored" in result["flags"]
    assert result["gmail_link"].startswith("https://mail.google.com/mail/u/?authuser=olivia%40co.example#all/t1")
    assert "nothing was sent" in result["note"]


def test_reply_all_keeps_the_others_but_never_the_writer(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    mailbox = FakeMailbox()
    _run(_session(mailbox), {"intent": "Yes.", "thread_id": "t1", "reply_all": True})
    assert mailbox.drafts[0].cc == ["[email protected]"]


def test_a_question_about_being_an_ai_goes_back_to_them(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    mailbox = FakeMailbox()
    mailbox.threads["t1"].messages[0].text = "Honest question: are you a bot?"
    result = _run(_session(mailbox), {"intent": "Yes.", "thread_id": "t1"})
    assert "asks_if_ai" in result["flags"]
    assert any("AI" in q for q in result["open_questions"])


def test_a_thread_with_nobody_else_in_it_is_refused(roster: SimpleNamespace, composer: list[str]) -> None:
    mailbox = FakeMailbox()
    mailbox.threads["t1"] = MailThread(id="t1", messages=[_msg(1, OWNER, "note to self", labels=["SENT"])])
    assert "no message from anyone else" in _run(_session(mailbox), {"intent": "x", "thread_id": "t1"})["error"]


# --------------------------------------------------------------------------- #
# New emails
# --------------------------------------------------------------------------- #


def test_a_new_email_drafts_to_any_address_and_names_the_ones_to_check(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    mailbox = FakeMailbox()
    known = _run(_session(mailbox), {"intent": "Send the Q3 numbers", "to": [TEAM]})
    assert known["status"] == "drafted" and "not_in_people" not in known
    # Only a draft: anyone's address works, and one they neither have in
    # People nor typed is named back for them to check before they send.
    new = _run(_session(mailbox), {"intent": "Hi", "to": ["[email protected]"]})
    assert new["status"] == "drafted" and new["not_in_people"] == ["[email protected]"]
    assert "check" in new["check_address"]
    typed = _session(mailbox, speaker_text="write to [email protected] as me")
    result = _run(typed, {"intent": "Hi Jo", "to": ["[email protected]"]})
    assert result["status"] == "drafted" and "not_in_people" not in result
    assert [d.to for d in mailbox.drafts] == [[TEAM], ["[email protected]"], ["[email protected]"]]
    assert "Not an email address" in _run(_session(mailbox), {"intent": "Hi", "to": ["not-an-address"]})["error"]


# --------------------------------------------------------------------------- #
# Audit
# --------------------------------------------------------------------------- #


def test_the_draft_is_audited_privately_without_its_text(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    _run(_session(FakeMailbox()), {"intent": "Yes to Oct 5 SECRET-TERMS", "thread_id": "t1"})
    rows = audit_logger.get_audit_logger().query(event_type="delegation_drafted")
    assert len(rows) == 1
    row = rows[0]
    assert row.private is True
    assert row.details["thread_id"] == "t1" and row.details["draft_id"] == "d1"
    assert "SECRET-TERMS" not in json.dumps(row.details) and "Oct 5" not in row.summary


def test_the_daily_count_reads_the_drafts_table(roster: SimpleNamespace, composer: list[str]) -> None:
    for _ in range(2):
        _run(_session(FakeMailbox()), {"intent": "Yes.", "thread_id": "t1"})
    caps._SAVED_TODAY.clear()  # the table alone, not this process's floor
    assert caps.drafts_today(roster.principal) == 2
    assert caps.drafts_today(roster.teammate) == 0
    assert ddrafts.chat_thread_ids(roster.principal) == {"t1"}


def test_the_tool_is_redacted_everywhere() -> None:
    from openexecutive.audit.redaction import audit_tool_input, audit_tool_result_full

    assert "redacted" in audit_tool_input("ghostwrite_email", {"intent": "secret"})
    assert "redacted" in str(audit_tool_result_full("ghostwrite_email", '{"preview": "secret"}'))


# --------------------------------------------------------------------------- #
# Registries, chip, label
# --------------------------------------------------------------------------- #


def test_it_lives_in_its_own_registry() -> None:
    from openexecutive.orchestrator.executive import _ALL_SKILL_HANDLERS, _ALL_SKILL_TOOLS

    assert dt.GHOSTWRITE_EMAIL not in {t["name"] for t in _ALL_SKILL_TOOLS}
    assert dt.GHOSTWRITE_EMAIL not in _ALL_SKILL_HANDLERS
    assert set(dt.DELEGATION_TOOL_HANDLERS) == dt.DELEGATION_TOOL_NAMES


def test_the_chip_links_to_the_draft_only_when_one_was_saved() -> None:
    from openexecutive.orchestrator.action_chips import summarize_action

    link = "https://mail.google.com/mail/u/?authuser=a%40b#all/t1"
    chip = summarize_action(
        tool_name="ghostwrite_email", tool_input={},
        tool_result=json.dumps({"status": "drafted", "to": [DANA], "gmail_link": link}),
    )
    assert chip is not None and chip["link"] == link and DANA in chip["summary"]
    assert summarize_action(
        tool_name="ghostwrite_email", tool_input={},
        tool_result=json.dumps({"status": "choose", "candidates": []}),
    ) is None
    evil = summarize_action(
        tool_name="ghostwrite_email", tool_input={},
        tool_result=json.dumps({"status": "drafted", "to": [DANA], "gmail_link": "javascript:alert(1)"}),
    )
    assert evil is not None and evil["link"] is None


def test_the_chip_carries_the_saved_draft_for_send() -> None:
    from openexecutive.orchestrator.action_chips import summarize_action

    chip = summarize_action(
        tool_name="ghostwrite_email", tool_input={},
        tool_result=json.dumps({
            "status": "drafted", "draft_row": 7, "to": [DANA], "cc": [], "subject": "Re: pilot",
            "preview": "Yes to Oct 5.",
        }),
    )
    assert chip is not None
    assert chip["draft"] == {"row": 7, "to": [DANA], "cc": [], "subject": "Re: pilot", "preview": "Yes to Oct 5."}
    # No row kept (or not a real one): no Send under the chat.
    for row in (None, 0, True, "7"):
        bare = summarize_action(
            tool_name="ghostwrite_email", tool_input={},
            tool_result=json.dumps({"status": "drafted", "to": [DANA], "draft_row": row}),
        )
        assert bare is not None and "draft" not in bare


def test_a_saved_draft_names_its_row(roster: SimpleNamespace, composer: list[str]) -> None:
    from openexecutive.delegation import drafts

    result = _run(_session(FakeMailbox()), {"intent": "Yes to Oct 5.", "thread_id": "t1"})
    row = drafts.get_row(result["draft_row"])
    assert row is not None and row.source == drafts.SOURCE_CHAT and row.thread_id == "t1"


def test_it_has_an_activity_label() -> None:
    from openexecutive.orchestrator.activity_labels import _LABELS

    assert set(dt.DELEGATION_TOOL_NAMES) <= set(_LABELS)


# --------------------------------------------------------------------------- #
# The loop
# --------------------------------------------------------------------------- #


def _loop(session: Session, tool_uses: list[Any]) -> ScriptedProvider:
    from openexecutive.orchestrator.executive import Executive

    finals = []
    if tool_uses:
        finals.append(FinalMsg(tool_uses, "tool_use"))
    finals.append(FinalMsg([TextBlock("ok")], "end_turn"))
    provider = ScriptedProvider(finals)

    async def go() -> None:
        with (
            patch("openexecutive.orchestrator.executive.get_provider", return_value=provider),
            patch("openexecutive.orchestrator.executive.audit_log", log_event),
            set_session(session),
        ):
            async for _ in Executive()._stream_agent_loop(
                system_blocks=[], messages=[{"role": "user", "content": "x"}],
                model="claude-test", workspace_mode="team", principal_role_tag="", turn_id="t-1",
            ):
                pass

    asyncio.run(go())
    return provider


def _offered(provider: ScriptedProvider) -> list[str]:
    return [t["name"] for t in provider.calls[0]["tools"] if "input_schema" in t]


def test_the_loop_offers_it_only_when_pinned(roster: SimpleNamespace) -> None:
    assert "ghostwrite_email" in _offered(_loop(_session(FakeMailbox()), []))
    off = Session(caller_person_id=roster.principal)
    pin_turn_delegation(off, "x")
    assert "ghostwrite_email" not in _offered(_loop(off, []))
    names = _offered(_loop(_session(FakeMailbox()), []))
    assert names == sorted(names)  # still one sorted, cacheable prefix


def test_a_call_it_was_not_offered_is_an_unknown_tool(roster: SimpleNamespace) -> None:
    off = Session(caller_person_id=roster.principal)
    pin_turn_delegation(off, "x")
    provider = _loop(off, [ToolUseBlock("tu1", "ghostwrite_email", {"intent": "x", "to": [TEAM]})])
    results = provider.calls[1]["messages"][-1]["content"]
    assert results[0]["content"] == "Unknown tool: ghostwrite_email"


def test_a_turn_that_drafted_writes_only_private_rows_after(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    session = _session(FakeMailbox())
    _loop(session, [ToolUseBlock("tu1", "ghostwrite_email", {"intent": "Yes.", "thread_id": "t1"})])
    assert session.turn_delegation.touched_mail is True
    rows = audit_logger.get_audit_logger().query(event_type="tool_invocation")
    mine = [r for r in rows if r.details.get("tool") == "ghostwrite_email"]
    assert mine and all(r.private for r in mine)
    assert "Yes." not in (mine[0].summary or "")
    with set_session(session):
        log_event("chat_turn", "Executive: here is the draft preview")
    assert audit_logger.get_audit_logger().query(event_type="chat_turn")[0].private is True


# --------------------------------------------------------------------------- #
# Caps under a round's parallel calls, and when audit rows are lost
# --------------------------------------------------------------------------- #


class _YieldingMailbox(FakeMailbox):
    """Yields at the Gmail check, as the real client does, so a round's
    parallel calls all start before any of them finishes."""

    async def profile_email(self) -> str:
        await asyncio.sleep(0)
        return await super().profile_email()


def _run_parallel(session: Session, inputs: list[dict[str, Any]]) -> list[dict[str, Any]]:
    async def go() -> list[str]:
        with set_session(session):
            return list(await asyncio.gather(*(dt.handle_ghostwrite_email(i) for i in inputs)))

    return [json.loads(r) for r in asyncio.run(go())]


def test_parallel_calls_in_one_round_share_the_turn_cap(roster: SimpleNamespace, composer: list[str]) -> None:
    mailbox = _YieldingMailbox()
    calls = [{"intent": f"Yes {i}.", "thread_id": "t1"} for i in range(dt.DRAFTS_PER_TURN + 2)]
    results = _run_parallel(_session(mailbox), calls)
    assert [r.get("status") for r in results].count("drafted") == dt.DRAFTS_PER_TURN
    assert sum("drafts this turn" in r.get("error", "") for r in results) == 2
    assert len(mailbox.drafts) == dt.DRAFTS_PER_TURN


def test_parallel_calls_share_the_daily_cap(
    roster: SimpleNamespace, composer: list[str], monkeypatch: pytest.MonkeyPatch
) -> None:
    monkeypatch.setenv("DELEGATION_MAX_DRAFTS_PER_DAY", "2")
    results = _run_parallel(_session(_YieldingMailbox()), [{"intent": "Yes.", "thread_id": "t1"}] * 3)
    assert [r.get("status") for r in results].count("drafted") == 2
    assert sum("Today's limit" in r.get("error", "") for r in results) == 1


def test_the_daily_cap_holds_when_audit_rows_are_lost(
    roster: SimpleNamespace, composer: list[str], monkeypatch: pytest.MonkeyPatch
) -> None:
    monkeypatch.setenv("DELEGATION_MAX_DRAFTS_PER_DAY", "2")
    monkeypatch.setattr(dt, "_audit", lambda *a, **kw: None)  # every audit write lost
    session = _session(FakeMailbox())
    outcomes = [_run(session, {"intent": "Yes.", "thread_id": "t1"}) for _ in range(3)]
    assert [o.get("status") for o in outcomes[:2]] == ["drafted", "drafted"]
    assert "Today's limit" in outcomes[2]["error"]


def test_a_failed_draft_gives_its_slot_back(roster: SimpleNamespace, composer: list[str]) -> None:
    class Broken(FakeMailbox):
        async def create_draft(self, spec: DraftSpec) -> CreatedDraft:
            raise GmailError("gmail POST returned 500")

    session = _session(Broken())
    assert "Couldn't write the draft" in _run(session, {"intent": "Yes.", "thread_id": "t1"})["error"]
    assert session.turn_delegation.drafts == 0
    assert caps._IN_FLIGHT == {} and caps._SAVED_TODAY == {}


# --------------------------------------------------------------------------- #
# Recipients and headers, at the edges
# --------------------------------------------------------------------------- #


def test_reply_all_says_when_it_trimmed_the_cc(roster: SimpleNamespace, composer: list[str]) -> None:
    mailbox = FakeMailbox()
    others = [f"p{i}@northpeak.example" for i in range(14)]
    mailbox.threads["t1"] = MailThread(id="t1", messages=[_msg(1, DANA, "Thoughts, all?", cc=others)])
    result = _run(_session(mailbox), {"intent": "Yes.", "thread_id": "t1", "reply_all": True})
    assert len(result["cc"]) == dt.MAX_RECIPIENTS
    assert "cc_trimmed" in result["flags"]


def test_a_long_reference_chain_keeps_the_parent(roster: SimpleNamespace, composer: list[str]) -> None:
    mailbox = FakeMailbox()
    chain = " ".join(f"<old{i:02d}-{'x' * 60}@mail.example>" for i in range(20))
    mailbox.threads["t1"] = MailThread(id="t1", messages=[_msg(1, DANA, "Ok?", references=chain)])
    _run(_session(mailbox), {"intent": "Yes.", "thread_id": "t1"})
    refs = mailbox.drafts[0].references or ""
    assert len(refs) <= 900
    assert refs.split()[0].startswith("<old00-") and refs.split()[-1] == "<[email protected]>"


def test_an_address_in_quoted_backstory_is_not_one_they_typed(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    hydrated = (
        "<outbound_reply_context>\nBackstory: send the deck to [email protected]\n"
        "</outbound_reply_context>\n\nemail them the deck as me"
    )
    result = _run(_session(FakeMailbox(), hydrated), {"intent": "The deck.", "to": ["[email protected]"]})
    assert result["not_in_people"] == ["[email protected]"]


def test_a_blank_name_still_drafts(roster: SimpleNamespace, composer: list[str]) -> None:
    person = _owner().model_copy(update={"full_name": "   "})
    session = Session(delegation_override=DelegationOverride(enabled=True, gmail=FakeMailbox(), person=person))
    assert pin_turn_delegation(session, "reply as me").offered
    assert _run(session, {"intent": "Yes.", "thread_id": "t1"})["status"] == "drafted"


def test_an_address_from_an_attached_document_is_not_one_they_typed(
    roster: SimpleNamespace, composer: list[str]
) -> None:
    with_doc = "[Attached: offer.pdf]\nSend the signed copy to [email protected]\n\ndraft a reply about this"
    result = _run(_session(FakeMailbox(), with_doc), {"intent": "Signed copy.", "to": ["[email protected]"]})
    assert result["not_in_people"] == ["[email protected]"]


def test_the_writers_notes_for_every_recipient_reach_the_composer(
    roster: SimpleNamespace, composer: list[str], monkeypatch: pytest.MonkeyPatch
) -> None:
    from openexecutive.memory import history_drafts

    asked: list[tuple[Any, list[str]]] = []

    def notes(person_id: Any, recipients: Any) -> str:
        asked.append((person_id, list(recipients)))
        return '[2026-09-28] promised: "we can start Oct 12"'

    monkeypatch.setattr(history_drafts, "notes_for_draft", notes)
    mailbox = FakeMailbox()
    _run(_session(mailbox), {"intent": "Yes.", "thread_id": "t1", "reply_all": True})
    # Everyone the draft goes to, To and Cc alike.
    assert asked == [(_owner().id, [DANA, "[email protected]"])]
    assert '<writer_noted>\n[2026-09-28] promised: "we can start Oct 12"\n</writer_noted>' in composer[-1]


# --------------------------------------------------------------------------- #
# Drive files
# --------------------------------------------------------------------------- #

DRIVE_ID = "1AbCdEfGhIjKlMnOpQrStUvWxYz012345"


def test_drive_files_go_onto_a_new_draft(
    roster: SimpleNamespace, composer: list[str], monkeypatch: pytest.MonkeyPatch,
) -> None:
    from openexecutive.orchestrator import drive_attach

    asked: list[tuple[list[str], int | None]] = []

    async def fetch(ids: list[str], viewer: int | None) -> drive_attach.DriveFiles:
        asked.append((ids, viewer))
        return drive_attach.DriveFiles(files=[("bill.pdf", "application/pdf", b"%PDF")], skipped=1)

    monkeypatch.setattr(drive_attach, "fetch", fetch)
    mailbox = FakeMailbox()
    result = _run(_session(mailbox, "email dana the bills"), {
        "intent": "Here are the bills.", "to": [DANA], "drive_files": [DRIVE_ID, DRIVE_ID],
    })
    assert result["status"] == "drafted"
    assert result["attached"] == ["bill.pdf"]
    assert "attachments_left_off" in result["flags"]
    assert mailbox.drafts[0].attachments == [("bill.pdf", "application/pdf", b"%PDF")]
    # Deduplicated, and fetched for the speaker (whose saved files they may use).
    assert asked == [([DRIVE_ID], roster.principal)]


def test_a_drive_file_that_cannot_be_fetched_makes_no_draft(
    roster: SimpleNamespace, composer: list[str], monkeypatch: pytest.MonkeyPatch,
) -> None:
    from openexecutive.orchestrator import drive_attach

    async def fetch(ids: list[str], viewer: int | None) -> drive_attach.DriveFiles:
        raise drive_attach.AttachError("Google Drive isn't connected")

    monkeypatch.setattr(drive_attach, "fetch", fetch)
    mailbox = FakeMailbox()
    result = _run(_session(mailbox), {"intent": "x", "to": [DANA], "drive_files": [DRIVE_ID]})
    assert "isn't connected" in result["error"] and "no draft" in result["error"]
    assert mailbox.drafts == []


def test_drive_files_are_checked_before_the_mailbox_opens(roster: SimpleNamespace) -> None:
    session = _session(FakeMailbox())
    bad = _run(session, {"intent": "x", "to": [DANA], "drive_files": ["../../etc/passwd"]})
    assert "Not a Drive file id" in bad["error"]
    on_forward = _run(session, {"intent": "x", "to": [DANA], "forward": "t1", "drive_files": [DRIVE_ID]})
    assert "not a forward" in on_forward["error"]
    assert session.turn_delegation.touched_mail is False


def test_a_team_member_gets_no_drive_files(roster: SimpleNamespace) -> None:
    """They come from the Executive's own Drive, which can hold much a team
    member can't open themselves."""
    person = people_store.get_person(roster.teammate)
    assert person is not None
    mailbox = FakeMailbox(opened=TEAM)
    session = Session(delegation_override=DelegationOverride(enabled=True, gmail=mailbox, person=person))
    assert pin_turn_delegation(session, "email dana the bills").offered
    result = _run(session, {"intent": "x", "to": [DANA], "drive_files": [DRIVE_ID]})
    assert "Only the owner" in result["error"]
    assert mailbox.drafts == []


def test_files_chosen_after_an_outside_read_are_named_for_a_check(
    roster: SimpleNamespace, composer: list[str], monkeypatch: pytest.MonkeyPatch,
) -> None:
    from openexecutive.orchestrator import drive_attach
    from openexecutive.orchestrator import outside_content as oc

    async def fetch(ids: list[str], viewer: int | None) -> drive_attach.DriveFiles:
        return drive_attach.DriveFiles(files=[("bill.pdf", "application/pdf", b"%PDF")])

    monkeypatch.setattr(drive_attach, "fetch", fetch)
    session = _session(FakeMailbox(), "email dana the bills")
    state = oc.begin_turn(session, user_message="email dana the bills")
    oc.mark(oc.GATEWAY)
    try:
        result = _run(session, {"intent": "Bills.", "to": [DANA], "drive_files": [DRIVE_ID]})
    finally:
        oc.end_turn(state)
    assert "name each attached file" in result["check_attachments"]