Skip to content

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

fix(delegation): stop drafts writing the sender's name twice above the signature (#535)

Rufusc049abba420a
diff --git a/packages/core/openexecutive/delegation/ghostwriter.py b/packages/core/openexecutive/delegation/ghostwriter.py
index 4c88f518..db78b4dd 100644
--- a/packages/core/openexecutive/delegation/ghostwriter.py
+++ b/packages/core/openexecutive/delegation/ghostwriter.py
@@ -181,6 +181,36 @@ def _shorten(text: str, cap: int) -> str:
return cut.rstrip()
+_WORD = re.compile(r"[^\W\d_]+(?:['’-][^\W\d_]+)*")
+
+
+def _drop_name_before_signature(body: str, signature: str) -> str:
+ """``body`` without a closing name that the signature's first line
+ already gives, so the name is not written twice: "Thanks,\nOlivia" above
+ "Olivia Owner" keeps "Thanks,", and "Best, Olivia" keeps "Best,"."""
+ first = next(
+ (line.strip() for line in signature.splitlines() if line.strip().strip("-").strip()), ""
+ )
+ sig_words = {w.casefold() for w in _WORD.findall(first)}
+
+ def is_name(text: str) -> bool:
+ words = _WORD.findall(text)
+ return (
+ 0 < len(words) <= 3
+ and all(w.casefold() in sig_words for w in words)
+ and not _WORD.sub("", text).strip(" .-–—~")
+ )
+
+ lines = body.rstrip().split("\n")
+ last = lines[-1].strip()
+ if len(lines) > 1 and is_name(last):
+ return "\n".join(lines[:-1]).rstrip()
+ head, comma, tail = last.rpartition(",")
+ if comma and head.strip() and len(_WORD.findall(head)) <= 4 and is_name(tail):
+ return "\n".join([*lines[:-1], head.rstrip() + ","])
+ return body
+
+
def _render_user_turn(
*,
writer_name: str,
@@ -303,7 +333,7 @@ async def compose(
# Everything it wrote was a link or an address the intent never gave.
raise ComposeError("the draft had nothing left once links and addresses were checked")
if signature.strip() and signature.strip() not in body:
- body = f"{body}\n\n{signature.strip()}"
+ body = f"{_drop_name_before_signature(body, signature)}\n\n{signature.strip()}"
subject = reply_subject if thread_text is not None and reply_subject else payload.get("subject")
questions = payload.get("open_questions")
return ComposedDraft(
diff --git a/packages/core/tests/unit/test_ghostwriter.py b/packages/core/tests/unit/test_ghostwriter.py
index 7915ab42..03716517 100644
--- a/packages/core/tests/unit/test_ghostwriter.py
+++ b/packages/core/tests/unit/test_ghostwriter.py
@@ -94,7 +94,7 @@ def test_a_reply_keeps_its_subject_gets_the_signature_and_loses_planted_links(
draft = _compose()
assert draft.subject == "Re: Brand refresh pilot"
assert "evil.example" not in draft.body
- assert draft.body.endswith("Olivia Owner\nFernway Studio")
+ assert draft.body.endswith("Best,\n\nOlivia Owner\nFernway Studio")
assert draft.flags == ["removed_link"]
assert draft.open_questions == ["Scope doc by Friday?"]
system, turn = calls[0]
@@ -105,6 +105,29 @@ def test_a_reply_keeps_its_subject_gets_the_signature_and_loses_planted_links(
assert "<intent>\nYes to Oct 5; day rate $1,500.\n</intent>" in turn
+ ("closing", "kept"),
+ [
+ ("Thanks,\nOlivia", "Thanks,"),
+ ("Best, Olivia", "Best,"),
+ ("Cheers,\n- Olivia Owner", "Cheers,"),
+ ("Thanks,\nDana", "Thanks,\nDana"),
+ ("Talk soon, Dana", "Talk soon, Dana"),
+ ],
+)
+def test_a_closing_name_the_signature_repeats_is_dropped(
+ monkeypatch: pytest.MonkeyPatch, closing: str, kept: str
+) -> None:
+ _model(monkeypatch, {"subject": "x", "body": f"Hi Dana,\n\nYes to Oct 5.\n\n{closing}"})
+ draft = _compose()
+ assert draft.body == f"Hi Dana,\n\nYes to Oct 5.\n\n{kept}\n\nOlivia Owner\nFernway Studio"
+
+
+def test_the_closing_name_stays_without_a_signature(monkeypatch: pytest.MonkeyPatch) -> None:
+ _model(monkeypatch, {"subject": "x", "body": "Hi Dana,\n\nYes.\n\nThanks,\nOlivia"})
+ assert _compose(signature="").body.endswith("Thanks,\nOlivia")
+
+
def test_the_writers_own_words_come_after_the_thread_in_their_own_block(
monkeypatch: pytest.MonkeyPatch,
) -> None: