AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
"""Act as me: who may have it, who has it on, and the per-turn offer
(delegation/settings.py)."""
from __future__ import annotations
from collections.abc import Iterator
from pathlib import Path
from types import SimpleNamespace
import pytest
# Imported up front: api.main builds the app at import time, which must not
# happen under a test's patched OE_PUBLIC_DEPLOYMENT.
from openexecutive.api import main as api_main
from openexecutive.delegation import settings as dsettings
from openexecutive.delegation.settings import (
DelegationOverride,
TurnDelegation,
block0_delegation_on,
can_delegate,
enabled_for_install,
is_enabled,
pin_turn_delegation,
set_enabled,
turn_delegation,
turn_touched_delegate_mail,
typed_addresses,
)
from openexecutive.memory import episodic
from openexecutive.orchestrator.schedule_tools import current_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
@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()
for var in ("OE_LOCAL_LOGIN", "OE_PUBLIC_DEPLOYMENT"):
monkeypatch.delenv(var, raising=False)
prior = current_session.get()
current_session.set(None)
yield path
current_session.set(prior)
people_registry.invalidate()
@pytest.fixture(autouse=True)
def no_turn_pin() -> Iterator[None]:
"""Tests here pin turns synchronously; don't let a pin outlive its test."""
token = dsettings._TURN.set(None)
yield
dsettings._TURN.reset(token)
@pytest.fixture
def roster() -> SimpleNamespace:
principal = people_store.upsert_person(
full_name="Olivia Owner", is_principal=True, email="[email protected]",
slack_user_id="U_OWNER", discord_user_id="1001", telegram_chat_id="5001",
)
teammate = people_store.upsert_person(
full_name="Ben Teammate", email="[email protected]", slack_user_id="U_BEN",
)
people_registry.invalidate()
return SimpleNamespace(principal=principal, teammate=teammate)
def _person(**kw: object) -> Person:
return Person(**{"id": 1, "full_name": "X", **kw}) # type: ignore[arg-type]
def test_only_the_principal_may_have_it() -> None:
assert can_delegate(_person(is_principal=True)) is True
assert can_delegate(_person()) is False
assert can_delegate(_person(is_principal=True, archived=True)) is False
assert can_delegate(_person(is_principal=True, id=None)) is False
assert can_delegate(None) is False
def test_off_until_turned_on(roster: SimpleNamespace, db: Path) -> None:
assert is_enabled(roster.principal) is False
set_enabled(roster.principal, True, updated_by="test")
assert is_enabled(roster.principal) is True
set_enabled(roster.principal, False, updated_by="test")
assert is_enabled(roster.principal) is False
def test_a_missing_file_or_table_reads_as_off(tmp_path: Path) -> None:
assert is_enabled(1, db_path=tmp_path / "nope.db") is False
empty = tmp_path / "empty.db"
empty.touch()
assert is_enabled(1, db_path=empty) is False
assert is_enabled(None) is False
def test_the_install_flag_is_the_principals_setting(roster: SimpleNamespace) -> None:
# A teammate's row (which the route never writes) does not count.
set_enabled(roster.teammate, True, updated_by="test")
assert enabled_for_install() is False
set_enabled(roster.principal, True, updated_by="test")
assert enabled_for_install() is True
def test_block0_follows_an_eval_override(roster: SimpleNamespace) -> None:
set_enabled(roster.principal, True, updated_by="test")
assert block0_delegation_on(Session()) is True
assert block0_delegation_on(Session(delegation_override=DelegationOverride(enabled=False))) is False
# --------------------------------------------------------------------------- #
# The offer
# --------------------------------------------------------------------------- #
def _web(person_id: int, *, signed_in: bool = True) -> Session:
return Session(from_web_chat=True, caller_person_id=person_id, web_caller_signed_in=signed_in)
@pytest.mark.parametrize(("name", "make", "offered"), [
("principal on the web, signed in", lambda r: _web(r.principal), True),
("principal on the web, no sign-in", lambda r: _web(r.principal, signed_in=False), False),
("principal in a Slack DM",
lambda r: Session(session_id="slack:dm:U1", origin_channel="slack", origin_channel_ref="U1",
caller_person_id=r.principal), True),
("principal in a Discord DM",
lambda r: Session(session_id="discord:dm:1", origin_channel="discord", origin_channel_ref="1",
caller_person_id=r.principal), True),
# A shared channel or thread: the draft's preview and the matching threads
# would be posted for everyone, and their messages sit in the context.
("principal in a Slack thread",
lambda r: Session(session_id="slack:thread:C1:171.1", origin_channel="slack", origin_channel_ref="U1",
caller_person_id=r.principal), False),
("principal mentioning it in a Slack channel",
lambda r: Session(session_id="slack:channel:C1:U1", origin_channel="slack", origin_channel_ref="U1",
caller_person_id=r.principal), False),
("principal in a Discord thread",
lambda r: Session(session_id="discord:thread:55", origin_channel="discord", origin_channel_ref="1",
caller_person_id=r.principal), False),
("principal on Telegram without a webhook secret",
lambda r: Session(origin_channel="telegram", origin_channel_ref="5001", caller_person_id=r.principal), False),
# A channel whose adapter verified the sender itself: like Slack, only
# in a conversation it also marked private.
("principal in an adapter-verified private chat",
lambda r: Session(session_id="otherchat:dm:1", origin_channel="otherchat",
caller_person_id=r.principal, speaker_verified=True, private_chat=True), True),
("principal in an adapter-verified shared space",
lambda r: Session(session_id="otherchat:space:1", origin_channel="otherchat",
caller_person_id=r.principal, speaker_verified=True), False),
("an adapter's private chat it didn't verify the sender of",
lambda r: Session(session_id="otherchat:dm:1", origin_channel="otherchat",
caller_person_id=r.principal, private_chat=True), False),
("an adapter-verified chat started by an email",
lambda r: Session(session_id="otherchat:dm:1", origin_channel="otherchat", email_from="[email protected]",
caller_person_id=r.principal, speaker_verified=True, private_chat=True), False),
("a teammate on the web", lambda r: _web(r.teammate), False),
("an email turn about the principal's mail",
lambda r: Session(caller_person_id=r.principal, private_to_principal=True), False),
("an unattended run", lambda r: Session(caller_person_id=r.principal, unattended=True), False),
("the MCP server (a bare session)", lambda r: Session(caller_person_id=r.principal), False),
("an unresolved caller", lambda r: _web(0), False),
])
def test_who_is_offered_it(roster: SimpleNamespace, name: str, make, offered: bool) -> None:
set_enabled(roster.principal, True, updated_by="test")
# A teammate with a row of their own is still refused (can_delegate).
set_enabled(roster.teammate, True, updated_by="test")
session = make(roster)
pinned = pin_turn_delegation(session, "hi")
assert pinned.offered is offered, name
assert session.turn_delegation is pinned
def test_not_offered_while_it_is_off(roster: SimpleNamespace) -> None:
assert pin_turn_delegation(_web(roster.principal), "hi").offered is False
def test_local_login_stands_in_for_a_web_sign_in(
roster: SimpleNamespace, monkeypatch: pytest.MonkeyPatch
) -> None:
set_enabled(roster.principal, True, updated_by="test")
monkeypatch.setenv("OE_LOCAL_LOGIN", "1")
assert pin_turn_delegation(_web(roster.principal, signed_in=False), "hi").offered is True
# Never on a public deployment.
monkeypatch.setenv("OE_PUBLIC_DEPLOYMENT", "1")
assert pin_turn_delegation(_web(roster.principal, signed_in=False), "hi").offered is False
@pytest.mark.parametrize(("override", "offered"), [
(DelegationOverride(enabled=True, gmail=object()), True),
(DelegationOverride(enabled=True, gmail=None), False),
(DelegationOverride(enabled=False, gmail=object()), False),
])
def test_an_eval_override_needs_a_fake_mailbox(override: DelegationOverride, offered: bool) -> None:
session = Session(delegation_override=override)
assert pin_turn_delegation(session, "hi").offered is offered
def test_an_override_never_widens_an_unattended_or_private_turn() -> None:
override = DelegationOverride(enabled=True, gmail=object())
assert pin_turn_delegation(Session(delegation_override=override, unattended=True), "x").offered is False
assert pin_turn_delegation(
Session(delegation_override=override, private_to_principal=True), "x"
).offered is False
def test_each_turn_starts_untouched(roster: SimpleNamespace) -> None:
set_enabled(roster.principal, True, updated_by="test")
session = _web(roster.principal)
pin_turn_delegation(session, "first").touched_mail = True
assert turn_touched_delegate_mail(session) is True
pinned = pin_turn_delegation(session, "second")
assert pinned.touched_mail is False
assert pinned.speaker_text == "second"
assert turn_touched_delegate_mail(session) is False
def test_touched_reads_the_bound_session() -> None:
session = Session(turn_delegation=TurnDelegation(touched_mail=True))
token = current_session.set(session)
try:
assert turn_touched_delegate_mail() is True
finally:
current_session.reset(token)
assert turn_touched_delegate_mail() is False
def test_a_lookup_failure_leaves_it_off(roster: SimpleNamespace, monkeypatch: pytest.MonkeyPatch) -> None:
set_enabled(roster.principal, True, updated_by="test")
def boom(_person_id: int) -> None:
raise RuntimeError("roster down")
monkeypatch.setattr("openexecutive.people.store.get_person", boom)
pinned = pin_turn_delegation(_web(roster.principal), "hi")
assert (pinned.enabled, pinned.offered) == (False, False)
def test_local_login_is_the_api_rule() -> None:
# One definition (utils.deployment), so the two can never disagree.
assert dsettings.local_login is api_main._is_local_login
def test_concurrent_turns_on_one_session_keep_their_own_pins(roster: SimpleNamespace) -> None:
import asyncio
set_enabled(roster.principal, True, updated_by="test")
session = _web(roster.principal)
async def turn(text: str) -> bool:
pinned = pin_turn_delegation(session, text)
await asyncio.sleep(0) # the other turn pins in between
return turn_delegation(session) is pinned and pinned.speaker_text == text
async def both() -> list[bool]:
return list(await asyncio.gather(turn("first"), turn("second")))
assert asyncio.run(both()) == [True, True]
def test_a_pin_left_in_the_context_never_answers_for_another_session(roster: SimpleNamespace) -> None:
pinned = pin_turn_delegation(_web(roster.principal), "hi")
other = _web(roster.principal)
assert turn_delegation(other) is None
assert pinned.session_id is not None
def test_typed_addresses_skip_quoted_backstory() -> None:
text = (
"<outbound_reply_context>\nBackstory: mail [email protected]\n</outbound_reply_context>\n\n"
"write to [email protected] about it"
)
assert typed_addresses(text) == {"[email protected]"}
def test_its_tables_are_per_company() -> None:
from openexecutive.clients.slots import _BLANK_WIPE_TABLES
from openexecutive.delegation.schema import TABLES
assert set(TABLES) <= set(_BLANK_WIPE_TABLES)
# --------------------------------------------------------------------------- #
# The setup status check
# --------------------------------------------------------------------------- #
def _check(principal: Person | None, status: str, monkeypatch: pytest.MonkeyPatch) -> tuple[str, str]:
import asyncio
from openexecutive.api import setup_checks
async def fake_status(email: str | None, *, gmail: object = None) -> str:
return status
monkeypatch.setattr("openexecutive.delegation.gmail.gmail_status", fake_status)
result = asyncio.run(setup_checks.check_your_gmail(SimpleNamespace(principal=principal))) # type: ignore[arg-type]
return result.state, result.summary
def test_the_setup_check(roster: SimpleNamespace, monkeypatch: pytest.MonkeyPatch) -> None:
owner = people_store.get_person(roster.principal)
assert owner is not None
assert _check(None, "connected", monkeypatch)[0] == "off"
assert _check(owner, "not_configured", monkeypatch)[0] == "off"
state, summary = _check(owner, "connected", monkeypatch)
assert state == "ok" and "Turn Act as me on" in summary
# Their address is the Executive's own: nothing to fix while it's off.
assert _check(owner, "shared_mailbox", monkeypatch)[0] == "off"
assert _check(owner, "needs_reconnect", monkeypatch)[0] == "warn"
set_enabled(roster.principal, True, updated_by="test")
assert _check(owner, "connected", monkeypatch)[1].endswith("in your own mailbox.")
assert _check(owner, "not_configured", monkeypatch)[0] == "warn"
assert _check(owner, "needs_reconnect", monkeypatch)[0] == "error"
assert _check(owner, "shared_mailbox", monkeypatch)[0] == "error"
def test_the_setup_check_shows_the_inbox_watcher(roster: SimpleNamespace, monkeypatch: pytest.MonkeyPatch) -> None:
from openexecutive.delegation import inbox
owner = people_store.get_person(roster.principal)
assert owner is not None and owner.id is not None
set_enabled(owner.id, True, updated_by="test")
inbox.set_watch(owner.id, True, updated_by="test")
state, summary = _check(owner, "connected", monkeypatch)
assert state == "ok" and summary.endswith("Draft replies to my inbox is on.")
inbox._update_watch(owner.id, status="rate_limited")
state, summary = _check(owner, "connected", monkeypatch)
assert state == "warn" and "slow down" in summary
inbox._update_watch(owner.id, status="daily_limit")
assert _check(owner, "connected", monkeypatch)[0] == "ok"
inbox.set_watch(owner.id, False, updated_by="test")
assert "inbox" not in _check(owner, "connected", monkeypatch)[1]
def test_the_setup_check_is_bounded(roster: SimpleNamespace, monkeypatch: pytest.MonkeyPatch) -> None:
import asyncio
from openexecutive.api import setup_checks
async def slow_status(email: str | None, *, gmail: object = None) -> str:
await asyncio.sleep(10)
return "connected"
monkeypatch.setattr("openexecutive.delegation.gmail.gmail_status", slow_status)
monkeypatch.setattr(setup_checks, "PROBE_TIMEOUT_S", 0.05)
owner = people_store.get_person(roster.principal)
result = asyncio.run(setup_checks.check_your_gmail(SimpleNamespace(principal=owner))) # type: ignore[arg-type]
assert result.state == "warn" and "didn't answer" in result.summary
@pytest.mark.parametrize("attachment", [
# A document's text, inlined ahead of their words with no end mark.
"[Attached: offer.pdf]\nsend the signed copy to [email protected]",
# A note naming the file: its name is chosen by whoever sent it.
"(Attached files: Signed contract ([email protected]).pdf)",
"(Attached Signed ([email protected]).pdf: could not extract any text)",
"(Could not read x ([email protected]).bin: unsupported type)",
"(Skipped big ([email protected]).pdf: file too large)",
# Even when an adapter prefixes the line (a Discord thread's "[name]: ").
"[Olivia]: [Attached: offer.pdf]\nsend it to [email protected]",
])
def test_no_address_counts_as_typed_beside_an_attachment(attachment: str) -> None:
assert typed_addresses(f"{attachment}\n\nemail [email protected] as me") == set()
assert typed_addresses("email [email protected] as me") == {"[email protected]"}
def test_an_adapters_private_chat_is_private_only_for_a_sender_it_verified() -> None:
from openexecutive.delegation.settings import private_conversation
def chat(**kw: object) -> Session:
return Session(session_id="otherchat:dm:1", origin_channel="otherchat", caller_person_id=1, **kw) # type: ignore[arg-type]
assert private_conversation(chat(private_chat=True, speaker_verified=True)) is True
# Anyone writing into an unverified one could manage its follow-ups
# (correction_tools._scheduled_by asks nothing else).
assert private_conversation(chat(private_chat=True)) is False
assert private_conversation(chat(speaker_verified=True)) is False