Skip to content

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

fix(chat): stop the host chat list growing once the mark is stored (#634)

Rufusefe9dd43bda6
diff --git a/packages/core/openexecutive/api/routes/chat.py b/packages/core/openexecutive/api/routes/chat.py
index 1cf6d6f5..77d12784 100644
--- a/packages/core/openexecutive/api/routes/chat.py
+++ b/packages/core/openexecutive/api/routes/chat.py
@@ -206,10 +206,11 @@ def _stop_owner_key(request: Request, caller_person_id: int | None) -> str:
return f"{key}|host" if api_caller.from_host(request) else key
-# Conversations a host origin's page has written in, as this process saw it
-# happen: the stored mark (session_store.host_wrote) can fail to write, and a
-# turn running alongside must be held all the same. The API runs as one
-# process.
+# Conversations a host origin's page has written in whose stored mark
+# (session_store.host_wrote) hasn't been written yet, or failed to write: a
+# turn running alongside must be held all the same. An id leaves once its mark
+# is stored, so a host page starting many chats doesn't grow it. The API runs
+# as one process.
_host_sessions: set[str] = set()
@@ -1301,7 +1302,8 @@ async def _run_chat_turn(
try:
from openexecutive.memory.session_store import mark_host_wrote
- mark_host_wrote(session.session_id)
+ if mark_host_wrote(session.session_id):
+ _host_sessions.discard(session.session_id)
except Exception:
logger.exception("chat.host_mark_failed session_id=%s", session.session_id)
diff --git a/packages/core/openexecutive/memory/session_store.py b/packages/core/openexecutive/memory/session_store.py
index 8e18a4c9..fa8841be 100644
--- a/packages/core/openexecutive/memory/session_store.py
+++ b/packages/core/openexecutive/memory/session_store.py
@@ -117,11 +117,13 @@ def save_message(
return int(cur.lastrowid or 0)
-def mark_host_wrote(session_id: str, db_path: Path = DB_PATH) -> None:
+def mark_host_wrote(session_id: str, db_path: Path = DB_PATH) -> bool:
"""Mark ``session_id`` as written in by a host origin's page, now, before
- its turn is saved (``host_wrote``). Never cleared."""
+ its turn is saved (``host_wrote``). Never cleared. Whether the mark is
+ stored: False when the conversation has no row to carry it."""
with _get_conn(db_path) as conn:
- conn.execute("UPDATE sessions SET host_wrote = 1 WHERE session_id = ?", (session_id,))
+ cur = conn.execute("UPDATE sessions SET host_wrote = 1 WHERE session_id = ?", (session_id,))
+ return cur.rowcount > 0
def host_wrote(session_id: str, db_path: Path = DB_PATH) -> bool:
diff --git a/packages/core/tests/integration/test_chat_host_surface.py b/packages/core/tests/integration/test_chat_host_surface.py
index 38ceec56..2ca9f1e0 100644
--- a/packages/core/tests/integration/test_chat_host_surface.py
+++ b/packages/core/tests/integration/test_chat_host_surface.py
@@ -228,3 +228,24 @@ def test_only_user_lines_from_the_host_are_marked(temp_db: Path) -> None:
assert not session_store.host_wrote("s1", db_path=temp_db)
session_store.save_message("s1", "user", "theirs", db_path=temp_db, sender_surface="host")
assert session_store.host_wrote("s1", db_path=temp_db)
+
+
+def test_a_stored_mark_frees_the_in_process_hold(temp_db: Path, seen: list[bool]) -> None:
+ """Once the mark is stored, the stored mark holds the chat and the id
+ leaves the process's own list, so a host page starting many chats
+ doesn't grow it."""
+ client = _client()
+ session_id = _session_id(client.post("/chat", json={"message": "plant"}, headers=HOST).text)
+ assert session_id not in chat_route._host_sessions
+ client.post("/chat", json={"message": "ok", "session_id": session_id})
+ assert seen == [True, True]
+
+
+def test_a_mark_with_no_row_keeps_the_hold(temp_db: Path, seen: list[bool], monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(session_store, "mark_host_wrote", lambda *_a, **_k: False)
+ monkeypatch.setattr(session_store, "host_wrote", lambda *_a, **_k: False)
+ client = _client()
+ session_id = _session_id(client.post("/chat", json={"message": "plant"}, headers=HOST).text)
+ assert session_id in chat_route._host_sessions
+ client.post("/chat", json={"message": "ok", "session_id": session_id})
+ assert seen == [True, True]