AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
fix(api): remove the unauthenticated /debug/last-turn endpoint (#200)
Rufus112486dfc6d5
diff --git a/packages/core/openexecutive/api/routes/chat.py b/packages/core/openexecutive/api/routes/chat.pyindex 9367cede..3e938794 100644--- a/packages/core/openexecutive/api/routes/chat.py+++ b/packages/core/openexecutive/api/routes/chat.py@@ -31,11 +31,6 @@ logger = logging.getLogger(__name__)_sessions: dict[str, Any] = {}-# Most-recent completed turn's debug events, for the /debug/last-turn endpoint.-# Single-process only; replaced wholesale at the end of every turn.-_last_turn_events: list[dict[str, Any]] = []-_last_turn_meta: dict[str, Any] = {}-_TITLE_MAX_LEN = 60@@ -112,7 +107,7 @@ class _StopEntry(NamedTuple):# user, and says `stopped`, because that is the word on the button. If you are# looking for every stoppable-async pattern in this codebase, grep both.#-# Single-process only — exactly like `_sessions` above and `_last_turn_*` below. A second+# Single-process only — exactly like `_sessions` above. A second# uvicorn worker would break in-flight session continuity before it broke this._active_stops: dict[str, _StopEntry] = {}@@ -794,7 +789,7 @@ async def _run_chat_turn(# aclosing is load-bearing, not decoration: `async for` does NOT# close its sub-iterator when the enclosing generator is closed. This# body used to BE event_generator, so a client disconnect ran its- # `finally` directly (the /debug/last-turn snapshot, and the+ # `finally` directly (releasing the stop switch, and the# Executive's upstream stream aclose). Wrapping it in a plain# `async for` would leave `_sse_body` suspended at its yield until a# later GC hop — or never, if the loop closes first.@@ -1140,19 +1135,6 @@ async def _run_chat_turn(done = json.dumps({"type": "done", "session_id": session.session_id})yield f"data: {done}\n\n"finally:- # Snapshot for /debug/last-turn.- global _last_turn_events, _last_turn_meta- _last_turn_events = [collector.to_sse_dict(e) for e in collector._events]- _last_turn_meta = {- "turn_id": turn_id,- "session_id": session.session_id,- "is_first_turn": is_first_turn,- "chunks": chunk_count,- "duration_s": round(time.monotonic() - exec_t0, 3),- "timed_out": timed_out,- "client_disconnected": client_disconnected,- "stopped": stopped,- }_release_stop(client_turn_id, turn_id)return StreamingResponse(@@ -1284,15 +1266,6 @@ async def chat_upload()[email protected]("/debug/last-turn")-def get_last_turn() -> dict[str, Any]:- """Returns the most recently completed turn's debug events.-- Single-process only — for local debugging when SSE itself failed.- """- return {"meta": _last_turn_meta, "events": _last_turn_events}--# ---------------------------------------------------------------------------# Suggested starter prompts for the chat empty-state.# ---------------------------------------------------------------------------diff --git a/packages/core/openexecutive/architecture/architecture-facts.yaml b/packages/core/openexecutive/architecture/architecture-facts.yamlindex 5398822e..f5bdff71 100644--- a/packages/core/openexecutive/architecture/architecture-facts.yaml+++ b/packages/core/openexecutive/architecture/architecture-facts.yaml@@ -2131,8 +2131,8 @@ chat:round-trip with a ~10s budget) is skipped on the stop path, so persistingfirst does not make the button feel dead.- `_active_stops` is single-process, exactly like `_sessions` and- `_last_turn_*` in the same module and `_active_cancellations` in+ `_active_stops` is single-process, exactly like `_sessions` in the+ same module and `_active_cancellations` inapi/routes/evals.py (the pattern this copies). A second uvicorn workerwould break in-flight session continuity before it broke stop. It isbounded (`_STOP_REGISTRY_MAX`) so a leaked entry cannot grow without limit.diff --git a/packages/core/tests/integration/test_chat_committee.py b/packages/core/tests/integration/test_chat_committee.pyindex 6df7e6d5..3a82f1d4 100644--- a/packages/core/tests/integration/test_chat_committee.py+++ b/packages/core/tests/integration/test_chat_committee.py@@ -17,8 +17,6 @@ from openexecutive.memory.company_profile import CompanyProfile@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()diff --git a/packages/core/tests/unit/test_chat_route_activity_events.py b/packages/core/tests/unit/test_chat_route_activity_events.pyindex 3705234c..8a8fe8fe 100644--- a/packages/core/tests/unit/test_chat_route_activity_events.py+++ b/packages/core/tests/unit/test_chat_route_activity_events.py@@ -41,8 +41,6 @@ SPECIALIST_ACTIVITY: dict[str, Any] = {@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()diff --git a/packages/core/tests/unit/test_chat_route_alert_trust.py b/packages/core/tests/unit/test_chat_route_alert_trust.pyindex 20d5b016..7fd4f39a 100644--- a/packages/core/tests/unit/test_chat_route_alert_trust.py+++ b/packages/core/tests/unit/test_chat_route_alert_trust.py@@ -29,8 +29,6 @@ from openexecutive.people import store as people_store@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()diff --git a/packages/core/tests/unit/test_chat_route_memory_text.py b/packages/core/tests/unit/test_chat_route_memory_text.pyindex eb02ba4b..3872596f 100644--- a/packages/core/tests/unit/test_chat_route_memory_text.py+++ b/packages/core/tests/unit/test_chat_route_memory_text.py@@ -25,8 +25,6 @@ from openexecutive.people import store as people_store@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()diff --git a/packages/core/tests/unit/test_chat_route_page_context.py b/packages/core/tests/unit/test_chat_route_page_context.pyindex 48840893..d28f73ba 100644--- a/packages/core/tests/unit/test_chat_route_page_context.py+++ b/packages/core/tests/unit/test_chat_route_page_context.py@@ -29,8 +29,6 @@ from openexecutive.people import store as people_store@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()diff --git a/packages/core/tests/unit/test_chat_route_parallel_context.py b/packages/core/tests/unit/test_chat_route_parallel_context.pyindex 0dbdaaac..fa07b939 100644--- a/packages/core/tests/unit/test_chat_route_parallel_context.py+++ b/packages/core/tests/unit/test_chat_route_parallel_context.py@@ -26,8 +26,6 @@ from openexecutive.people import store as people_store@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()diff --git a/packages/core/tests/unit/test_chat_route_persistence.py b/packages/core/tests/unit/test_chat_route_persistence.pyindex 2ae9ac36..6fca7bb3 100644--- a/packages/core/tests/unit/test_chat_route_persistence.py+++ b/packages/core/tests/unit/test_chat_route_persistence.py@@ -46,8 +46,6 @@ def _all_sessions(db_path: Path) -> list[dict[str, Any]]:@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()chat_route._active_stops.clear()diff --git a/packages/core/tests/unit/test_chat_route_principal.py b/packages/core/tests/unit/test_chat_route_principal.pyindex d91cf468..0d9d8395 100644--- a/packages/core/tests/unit/test_chat_route_principal.py+++ b/packages/core/tests/unit/test_chat_route_principal.py@@ -22,8 +22,6 @@ from openexecutive.people import store as people_store@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()diff --git a/packages/core/tests/unit/test_chat_route_stop.py b/packages/core/tests/unit/test_chat_route_stop.pyindex e6f01f0c..1c9d5674 100644--- a/packages/core/tests/unit/test_chat_route_stop.py+++ b/packages/core/tests/unit/test_chat_route_stop.py@@ -63,8 +63,6 @@ def _events(body: str) -> list[dict[str, Any]]:@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()chat_route._active_stops.clear()diff --git a/packages/core/tests/unit/test_chat_route_timeout.py b/packages/core/tests/unit/test_chat_route_timeout.pyindex 3d14d2ac..7d0c6d16 100644--- a/packages/core/tests/unit/test_chat_route_timeout.py+++ b/packages/core/tests/unit/test_chat_route_timeout.py@@ -8,6 +8,7 @@ If `Executive.stream_chat` hangs, the route must:from __future__ import annotationsimport asyncio+import jsonimport sqlite3from collections.abc import AsyncIteratorfrom pathlib import Path@@ -36,8 +37,6 @@ def _all_sessions(db_path: Path) -> list[dict[str, Any]]:@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()chat_route._active_stops.clear()@@ -107,9 +106,11 @@ def test_chat_endpoint_handles_hanging_executive(assert rows[0]["title"] == "trigger hang"-def test_last_turn_debug_endpoint_returns_events(+def test_debug_events_reach_only_the_callers_own_stream(temp_db: Path, patched_deps: None, monkeypatch: pytest.MonkeyPatch) -> None:+ """A turn's debug events go to the caller over their own SSE stream. There+ is no process-wide "last turn" snapshot any other caller could read (#198)."""from openexecutive.orchestrator import executive as exec_modclass _MiniExecutive:@@ -129,14 +130,16 @@ def test_last_turn_debug_endpoint_returns_events(chat_resp = client.post("/chat", json={"message": "hello"})assert chat_resp.status_code == 200- _ = chat_resp.text-- last = client.get("/debug/last-turn").json()- assert last["meta"]["chunks"] == 1- kinds = [e["kind"] for e in last["events"]]+ events = [+ json.loads(line[len("data: "):])+ for line in chat_resp.text.splitlines()+ if line.startswith("data: ")+ ]+ debug = [e for e in events if e.get("type") == "debug_event"]+ kinds = [e["kind"] for e in debug]assert "knowledge_retrieved" in kindsassert "turn_complete" in kinds# All events carry the same turn_id.- turn_ids = {e.get("turn_id") for e in last["events"]}- assert len(turn_ids) == 1- assert next(iter(turn_ids)) == last["meta"]["turn_id"]+ assert len({e.get("turn_id") for e in debug}) == 1++ assert client.get("/debug/last-turn").status_code == 404diff --git a/packages/core/tests/unit/test_chat_route_turn_attribution.py b/packages/core/tests/unit/test_chat_route_turn_attribution.pyindex 74a463cc..159337d1 100644--- a/packages/core/tests/unit/test_chat_route_turn_attribution.py+++ b/packages/core/tests/unit/test_chat_route_turn_attribution.py@@ -41,8 +41,6 @@ from openexecutive.memory.company_profile import CompanyProfile@pytest.fixture(autouse=True)def _reset_route_state() -> None:chat_route._sessions.clear()- chat_route._last_turn_events.clear()- chat_route._last_turn_meta.clear()@pytest.fixture()