AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
fix(delegation): resolve the Act as me caller through the shared caller resolver (#478)
Rufus7e0449651823
diff --git a/packages/core/openexecutive/api/routes/delegation.py b/packages/core/openexecutive/api/routes/delegation.pyindex f761b85a..7dc36bf2 100644--- a/packages/core/openexecutive/api/routes/delegation.py+++ b/packages/core/openexecutive/api/routes/delegation.py@@ -487,19 +487,24 @@ def _refuse(status_code: int, code: str, message: str) -> HTTPException:def _caller(request: Request) -> Person:- """The signed-in caller, when they may have Act as me; else 403."""- from openexecutive.people.store import find_person_by_email, find_principal_person+ """The signed-in caller, when they may have Act as me; else 403. Found+ through the app's one caller resolver (``chat._resolve_caller_person_id``),+ so a deployment that maps a signed-in account to its person there is+ followed here too. A request that names no one only counts as the+ principal under local login."""+ from openexecutive.api.routes.chat import _resolve_caller_person_id+ from openexecutive.people.store import find_principal_person, get_personwho = api_caller.caller(request)+ if not who.email and not (who.defaults_to_principal and local_login()):+ raise _refuse(403, "sign_in_required", "Sign in to change Act as me.")try:- if who.email:- person = find_person_by_email(who.email)- elif who.defaults_to_principal and local_login():- person = find_principal_person()- else:- raise _refuse(403, "sign_in_required", "Sign in to change Act as me.")- except HTTPException:- raise+ person_id = _resolve_caller_person_id(request)+ if person_id is None:+ # The resolver reads a roster it can't open as "no one"; read it+ # once more so an outage answers 503, not "not for you".+ find_principal_person()+ person = get_person(person_id) if person_id is not None else Noneexcept Exception as exc:logger.exception("delegation: caller lookup failed")raise _refuse(503, "roster_unavailable", "Couldn't read the People list.") from excdiff --git a/packages/core/openexecutive/api/routes/may_do.py b/packages/core/openexecutive/api/routes/may_do.pyindex bb392a4c..c72e5006 100644--- a/packages/core/openexecutive/api/routes/may_do.py+++ b/packages/core/openexecutive/api/routes/may_do.py@@ -64,7 +64,7 @@ def _viewer(request: Request) -> Any:its person there is followed here too. A request that names no one onlycounts as the principal under local login."""from openexecutive.api.routes.chat import _resolve_caller_person_id- from openexecutive.people.store import get_person+ from openexecutive.people.store import find_principal_person, get_personfrom openexecutive.utils.deployment import is_local_loginwho = api_caller.caller(request)@@ -72,6 +72,10 @@ def _viewer(request: Request) -> Any:raise HTTPException(status_code=403, detail="Sign in to see what it may do.")try:person_id = _resolve_caller_person_id(request)+ if person_id is None:+ # The resolver reads a roster it can't open as "no one"; read it+ # once more so an outage answers 503, not "not for you".+ find_principal_person()person = get_person(person_id) if person_id is not None else Noneexcept Exception as exc:logger.exception("may_do: caller lookup failed")diff --git a/packages/core/tests/unit/test_delegation_route.py b/packages/core/tests/unit/test_delegation_route.pyindex c305812b..ecd678e7 100644--- a/packages/core/tests/unit/test_delegation_route.py+++ b/packages/core/tests/unit/test_delegation_route.py@@ -93,6 +93,39 @@ def test_a_request_without_a_sign_in_is_refused(assert client.get("/delegation").status_code == 200+def _roster_down(monkeypatch: pytest.MonkeyPatch) -> None:+ import sqlite3+ from contextlib import contextmanager++ @contextmanager+ def broken(db_path: Any = None) -> Iterator[Any]:+ raise sqlite3.OperationalError("database is locked")+ yield++ monkeypatch.setattr(people_store, "_get_conn", broken)+++def test_a_roster_it_cannot_read_says_so(+ client: TestClient, ids: dict[str, int], monkeypatch: pytest.MonkeyPatch+) -> None:+ _roster_down(monkeypatch)+ resp = client.get("/delegation", headers=OWNER)+ assert resp.status_code == 503 and resp.json()["detail"]["code"] == "roster_unavailable"+++def test_it_knows_the_caller_the_way_the_rest_of_the_app_does(+ client: TestClient, ids: dict[str, int], monkeypatch: pytest.MonkeyPatch+) -> None:+ # A deployment can map a signed-in account to its person in the shared+ # caller resolver; Act as me must follow that, not look the email up again.+ monkeypatch.setattr(+ "openexecutive.api.routes.chat._resolve_caller_person_id", lambda request: ids["principal"],+ )+ resp = client.get("/delegation", headers={"x-caller-email": "[email protected]"})+ assert resp.status_code == 200+ assert "--email [email protected]" in resp.json()["gmail"]["connect_command"]++@pytest.mark.parametrize(("status", "code"), [("not_configured", "gmail_not_connected"),("needs_reconnect", "gmail_needs_reconnect"),diff --git a/packages/core/tests/unit/test_may_do.py b/packages/core/tests/unit/test_may_do.pyindex b50e3f7e..eb06a5f4 100644--- a/packages/core/tests/unit/test_may_do.py+++ b/packages/core/tests/unit/test_may_do.py@@ -257,6 +257,25 @@ def test_the_owner_sees_all_three(client: TestClient, ids: dict[str, int]) -> Noassert body["paused"] is False+def _roster_down(monkeypatch: pytest.MonkeyPatch) -> None:+ import sqlite3+ from contextlib import contextmanager++ @contextmanager+ def broken(db_path: Any = None) -> Iterator[Any]:+ raise sqlite3.OperationalError("database is locked")+ yield++ monkeypatch.setattr(people_store, "_get_conn", broken)+++def test_a_roster_it_cannot_read_says_so(+ client: TestClient, ids: dict[str, int], monkeypatch: pytest.MonkeyPatch+) -> None:+ _roster_down(monkeypatch)+ assert client.get("/what-it-may-do", headers=OWNER).status_code == 503++def test_it_knows_people_the_way_the_rest_of_the_app_does(client: TestClient, ids: dict[str, int], monkeypatch: pytest.MonkeyPatch) -> None: