AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
fix(briefing): label the principal's own chat messages in the brief (#515)
Rufus1888e3c48a14
diff --git a/packages/core/openexecutive/briefing/live_signals.py b/packages/core/openexecutive/briefing/live_signals.pyindex 7dc81731..3b8688d4 100644--- a/packages/core/openexecutive/briefing/live_signals.py+++ b/packages/core/openexecutive/briefing/live_signals.py@@ -92,15 +92,19 @@ def _local_hhmm(iso: str | None, tz: tzinfo, now: datetime | None = None) -> strreturn f"{local:%H:%M}"-def _principal_addresses() -> set[str]:- """The principal's own email addresses, lowercased. Empty on failure."""+def _principal() -> Any:+ """The principal's People row, or None (also on failure)."""from openexecutive.people.store import find_principal_persontry:- principal = find_principal_person()+ return find_principal_person()except Exception:logger.exception("live_signals: principal lookup failed")- return set()+ return None+++def _principal_addresses(principal: Any) -> set[str]:+ """The principal's own email addresses, lowercased."""if principal is None:return set()return {@@ -110,6 +114,33 @@ def _principal_addresses() -> set[str]:}+# The chat-app ids an inbound row's details can carry, against the People+# field that holds the same id.+_CHAT_IDS = (+ ("slack_user", "slack_user_id"),+ ("discord_user", "discord_user_id"),+ ("chat_id", "telegram_chat_id"),+)+++def _is_principal_chat(details: dict[str, Any], sender: str, principal: Any) -> bool:+ """Whether a chat-app inbound row is the principal writing to the Executive.++ Matched by People id where the adapter logs one, then by the chat app's+ own id for them, then by name: the account-mode adapters log the People+ name, so the principal's own chat read as a stranger by that name."""+ if principal is None:+ return False+ if principal.id is not None and details.get("sender_person_id") == principal.id:+ return True+ for detail_key, person_field in _CHAT_IDS:+ own = getattr(principal, person_field, None)+ if own and str(details.get(detail_key) or "") == str(own):+ return True+ name = " ".join(str(principal.full_name or "").split()).lower()+ return bool(name) and sender.lower() == name++def _is_roster_sender(address: str) -> bool:from openexecutive.people.store import find_person_by_email@@ -181,7 +212,8 @@ def _inbound(stuck_seen: set[str] = set()seen_ids: set[str] = set()total = 0- principal = _principal_addresses()+ principal_person = _principal()+ principal = _principal_addresses(principal_person)unanswered: set[str] | None = Nonereplies: dict[str, str] | None = Nonefor ev in rows: # newest first@@ -236,6 +268,11 @@ def _inbound(else:head, _, text = summary.partition(": ")sender = _one_line(_PAREN_REF_RE.sub("", head[len("Inbound "):]), 80)+ # "google_chat from Ann": the line names the channel already.+ if channel and sender.startswith(f"{channel} from "):+ sender = sender[len(f"{channel} from "):]+ if _is_principal_chat(details, sender, principal_person):+ sender = "the principal"topic = _one_line(text, 100)group_key = (sender.lower(), re.sub(r"^(re|fwd?):\s*", "", topic.lower()))group = groups.get(group_key)@@ -263,7 +300,7 @@ def _inbound(if g["preview"]:line += f" — \"{g['preview']}\""else:- line += f" {g['sender']}: {g['topic']}"+ line += f" from {g['sender']}: {g['topic']}"if g["count"] > 1:line += f" ({g['count']} messages)"lines.append(line)diff --git a/packages/core/openexecutive/briefing/narrative.py b/packages/core/openexecutive/briefing/narrative.pyindex 0a17b2d3..34d91749 100644--- a/packages/core/openexecutive/briefing/narrative.py+++ b/packages/core/openexecutive/briefing/narrative.py@@ -48,7 +48,8 @@ GROUNDING_RULE = (# Shared by the standalone briefs: who "you" is. The context names the-# principal (THE PRINCIPAL line, "from the principal" on their own mail), and+# principal (THE PRINCIPAL line, "from the principal" on their own mail and+# chat), and# without this the brief called its own reader by name and handed back, as# their to-dos, the asks they had mailed the Executive.READER_NAME_RULE = (@@ -57,7 +58,7 @@ READER_NAME_RULE = ("line, the reflection's notes, the activity).")READER_RULE = READER_NAME_RULE + (- " Mail from the principal is an ask of you, "+ " Mail or a chat message from the principal is an ask of you, ""the Executive: say what you did with it, and never list it as something ""they must do or reply to. An email marked (the Executive replied) has ""had its answer, so its sender is not waiting on anyone."diff --git a/packages/core/tests/unit/test_live_signals.py b/packages/core/tests/unit/test_live_signals.pyindex f305f51b..65f35776 100644--- a/packages/core/tests/unit/test_live_signals.py+++ b/packages/core/tests/unit/test_live_signals.py@@ -378,6 +378,55 @@ def test_the_principals_own_mail_is_labelled_as_theirs(assert roster.principal not in line+def test_the_principals_own_chat_is_labelled_as_theirs(+ audit: AuditLogger, roster: SimpleNamespace,+) -> None:+ """The principal's Google Chat messages to the Executive read to the brief+ as a stranger by their name, 'messaging you nonstop since noon'."""+ from openexecutive.people import store as people_store++ principal = people_store.find_principal_person()+ assert principal is not None+ # Account mode logs the People row's id and name.+ audit.log(+ "integration_inbound",+ "Inbound google_chat from Pat Owner (space=spaces/AAA): any word on access?",+ actor="google_chat",+ details={"channel": "google_chat", "sender_person_id": principal.id,+ "message_name": "spaces/AAA/messages/1"},+ )+ # Bot mode logs only the chat app's display name.+ audit.log(+ "integration_inbound",+ "Inbound google_chat from pat owner (space=spaces/BBB): the St. Albans offer",+ actor="google_chat", details={"channel": "google_chat"},+ )+ # A chat app name the roster doesn't use, matched by the app's own id.+ people_store.upsert_person(+ full_name="Pat Owner", email="[email protected]", is_principal=True, telegram_chat_id="42",+ person_id=principal.id,+ )+ audit.log(+ "integration_inbound",+ "Inbound telegram from Patty (chat_id=42): where is the deck",+ actor="telegram", details={"channel": "telegram", "chat_id": 42, "sender": "Patty"},+ )+ audit.log(+ "integration_inbound",+ "Inbound google_chat from Dana Lee (space=spaces/CCC): budget is in",+ actor="google_chat", details={"channel": "google_chat", "sender_person_id": 999},+ )++ joined = "\n".join(live_signals.gather_live_signals(_since(), include_private=False).inbound)++ assert "telegram from the principal: where is the deck" in joined+ assert "google_chat from the principal: the St. Albans offer" in joined+ assert "google_chat from the principal: any word on access?" in joined+ assert "google_chat from Dana Lee: budget is in" in joined+ assert "Pat Owner" not in joined and "Patty" not in joined+ assert "google_chat google_chat" not in joined++def test_a_roster_email_says_whether_it_got_its_reply(audit: AuditLogger, roster: SimpleNamespace,) -> None: