AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).
Close adversarial review findings on the attribution fix
Rufusa8a3cb6a6f47
diff --git a/packages/core/openexecutive/alerts/pipeline.py b/packages/core/openexecutive/alerts/pipeline.pyindex d32d5994..aa4e1c37 100644--- a/packages/core/openexecutive/alerts/pipeline.py+++ b/packages/core/openexecutive/alerts/pipeline.py@@ -272,13 +272,17 @@ async def _evaluate_in_turn(audit_turn_id: str | None,) -> None:"""Re-bind the scheduling caller's audit ids, then evaluate."""- from openexecutive.audit.context import set_turn+ from openexecutive.audit.context import get_active_ids, set_turn- if audit_session_id is None and audit_turn_id is None:- # Nothing to restore — and binding (None, None) would clobber any- # ambient turn rather than preserve it.+ # Per-field fallback — see memory.episodic.extract_and_store for why a+ # half-empty snapshot must not erase the ambient counterpart.+ ambient_session, ambient_turn = get_active_ids()+ effective_session = audit_session_id if audit_session_id is not None else ambient_session+ effective_turn = audit_turn_id if audit_turn_id is not None else ambient_turn++ if (effective_session, effective_turn) == (ambient_session, ambient_turn):await evaluate_and_dispatch(event)return- with set_turn(session_id=audit_session_id, turn_id=audit_turn_id):+ with set_turn(session_id=effective_session, turn_id=effective_turn):await evaluate_and_dispatch(event)diff --git a/packages/core/openexecutive/api/routes/chat.py b/packages/core/openexecutive/api/routes/chat.pyindex 30281a94..4549aff5 100644--- a/packages/core/openexecutive/api/routes/chat.py+++ b/packages/core/openexecutive/api/routes/chat.py@@ -39,11 +39,40 @@ _last_turn_meta: dict[str, Any] = {}_TITLE_MAX_LEN = 60+# Session ids are client-supplied (a JSON field on /chat, a form field on+# /chat/upload) and are now bound as the audit session for the WHOLE turn, so+# they reach every audit and usage row the turn produces — and the route's own+# log lines. An unconstrained value is therefore a log-injection vector (a+# newline forges a log record) and makes mis-attribution trivially easy.+# Same charset as api.routes.audit._SESSION_ID_RE, which already had to+# defend the read side against integration-derived ids; it is wide enough for+# every real form ("slack:thread:C1:1700000000.001", "email:x@host", uuid4).+_SESSION_ID_RE = re.compile(r"^[A-Za-z0-9_:@\-\.\+/=]{1,256}$")+++def _clean_session_id(session_id: str | None) -> str | None:+ """Drop a client-supplied session id that isn't a plausible id.++ Dropped rather than rejected: a malformed id is indistinguishable from a+ stale client, and minting a fresh session keeps the turn working while+ denying the attacker a chosen audit key.+ """+ if session_id is None:+ return None+ if not _SESSION_ID_RE.match(session_id):+ logger.warning(+ "chat.session_id_rejected len=%d", len(session_id),+ )+ return None+ return session_id++def _get_or_create_session(session_id: str | None, request: Request) -> Any:from openexecutive.memory.session_store import load_messagesfrom openexecutive.onboarding.profile_builder import load_or_create_profilefrom openexecutive.orchestrator.session import Session+ session_id = _clean_session_id(session_id)if session_id and session_id in _sessions:return _sessions[session_id]@@ -172,7 +201,9 @@ async def _run_chat_turn(from openexecutive.orchestrator.executive import Executivet0 = time.monotonic()- turn_id = uuid.uuid4().hex+ # Same shape every other entry point mints (Executive.stream_chat), so+ # an audit query filtering on the `t-` prefix sees SSE turns too.+ turn_id = f"t-{uuid.uuid4().hex[:12]}"collector = DebugCollector(t0=t0, turn_id=turn_id)session = _get_or_create_session(session_id, request)@@ -326,9 +357,17 @@ async def _run_chat_turn(# Bound out here, in the generator Starlette itself drives, every# step inherits it. `set_turn` saves and restores rather than using# Token.reset precisely so it survives that task-hopping.+ # 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+ # 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.with set_turn(session_id=session.session_id, turn_id=turn_id):- async for evt in _sse_body():- yield evt+ async with contextlib.aclosing(_sse_body()) as body:+ async for evt in body:+ yield evtasync def _sse_body():from openexecutive.memory.session_store import (diff --git a/packages/core/openexecutive/memory/episodic.py b/packages/core/openexecutive/memory/episodic.pyindex 99e02cac..6147e0b0 100644--- a/packages/core/openexecutive/memory/episodic.py+++ b/packages/core/openexecutive/memory/episodic.py@@ -1861,19 +1861,25 @@ async def extract_and_store(caller's `with set_turn(...)` has already exited, so without them thisfunction's model call records unattributed."""- from openexecutive.audit.context import set_turn-- if audit_session_id is None and audit_turn_id is None:- # Nothing to restore. Binding (None, None) here would CLOBBER an- # ambient turn for a caller that awaits this directly from inside- # its own `with set_turn(...)` — the exact opposite of the bug this- # parameter exists to fix.+ from openexecutive.audit.context import get_active_ids, set_turn++ # Fall back per field, not as a pair. Binding a half-empty snapshot+ # would erase the ambient counterpart — a row under a session with no+ # turn, or a turn that joins to nothing — which is worse than either+ # binding both or leaving the ambient values alone. With nothing to+ # restore at all this is a plain no-op, so a caller that awaits this+ # directly from inside its own `with set_turn(...)` keeps its binding.+ ambient_session, ambient_turn = get_active_ids()+ effective_session = audit_session_id if audit_session_id is not None else ambient_session+ effective_turn = audit_turn_id if audit_turn_id is not None else ambient_turn++ if (effective_session, effective_turn) == (ambient_session, ambient_turn):await _extract_and_store(user_message, assistant_response, db_path, session_id)return- with set_turn(session_id=audit_session_id, turn_id=audit_turn_id):+ with set_turn(session_id=effective_session, turn_id=effective_turn):await _extract_and_store(user_message, assistant_response, db_path, session_id)diff --git a/packages/core/openexecutive/orchestrator/executive.py b/packages/core/openexecutive/orchestrator/executive.pyindex bf82a7d6..5f36d230 100644--- a/packages/core/openexecutive/orchestrator/executive.py+++ b/packages/core/openexecutive/orchestrator/executive.py@@ -561,7 +561,9 @@ class Executive:# A caller-supplied id wins: the SSE route binds the turn at route# level and must agree with us, or one turn splits across two ids.# Every other entry point passes nothing and keeps its own id.- turn_id = turn_id or f"t-{uuid.uuid4().hex[:12]}"+ # `is None`, not `or`: an explicitly-passed empty string would+ # otherwise silently mint a second id and split one turn in two.+ turn_id = f"t-{uuid.uuid4().hex[:12]}" if turn_id is None else turn_idt0 = time.monotonic()full_response = ""@@ -778,7 +780,9 @@ class Executive:# A caller-supplied id wins: the SSE route binds the turn at route# level and must agree with us, or one turn splits across two ids.# Every other entry point passes nothing and keeps its own id.- turn_id = turn_id or f"t-{uuid.uuid4().hex[:12]}"+ # `is None`, not `or`: an explicitly-passed empty string would+ # otherwise silently mint a second id and split one turn in two.+ turn_id = f"t-{uuid.uuid4().hex[:12]}" if turn_id is None else turn_id# Stash on the function frame so the closing audit_log("committee_review")# at the end of this function can carry the same turn_id._committee_turn_id = turn_iddiff --git a/packages/core/tests/unit/test_audit_context.py b/packages/core/tests/unit/test_audit_context.pyindex 2633e39c..3126ee8f 100644--- a/packages/core/tests/unit/test_audit_context.py+++ b/packages/core/tests/unit/test_audit_context.py@@ -202,3 +202,65 @@ def test_direct_extraction_does_not_clobber_an_ambient_turn() -> None:asyncio.run(_go())assert seen == [("s-amb", "t-amb")]+++def test_thread_fallback_carries_the_turn() -> None:+ """The branch the snapshot parameters actually exist for.++ In the event-loop branch `loop.create_task` copies the context at+ creation, so the child already inherits the ids and the re-bind is+ belt-and-braces. A thread started by the no-running-loop fallback gets+ a FRESH, EMPTY context and inherits nothing — so without the explicit+ snapshot its model call records unattributed. Asserting only on the+ task branch would pass with the whole mechanism removed.+ """+ import threading++ from openexecutive.audit.context import get_active_ids, set_turn+ from openexecutive.memory import episodic++ seen: list[tuple[str | None, str | None]] = []+ done = threading.Event()++ async def _fake_extract(*_a: object, **_k: object) -> None:+ seen.append(get_active_ids())+ done.set()++ # No running loop here, so schedule_extraction takes the thread branch.+ with (+ mock.patch.object(episodic, "_extract_and_store", _fake_extract),+ set_turn(session_id="s-thread", turn_id="t-thread"),+ ):+ episodic.schedule_extraction("u", "a", session_id="s-thread")+ assert done.wait(timeout=5), "extraction thread never ran"++ assert seen == [("s-thread", "t-thread")]+++def test_partial_snapshot_does_not_erase_the_ambient_counterpart() -> None:+ """A half-empty snapshot must not null out the other field.++ Binding (session, None) over a live turn yields a row that shows up in+ the session view attached to no turn — worse than both binding both and+ leaving the ambient pair alone.+ """+ from openexecutive.audit.context import get_active_ids, set_turn+ from openexecutive.memory import episodic++ seen: list[tuple[str | None, str | None]] = []++ async def _fake_extract(*_a: object, **_k: object) -> None:+ seen.append(get_active_ids())++ async def _go() -> None:+ with (+ mock.patch.object(episodic, "_extract_and_store", _fake_extract),+ set_turn(session_id="s-live", turn_id="t-live"),+ ):+ # Session supplied, turn omitted — the ambient turn must survive.+ await episodic.extract_and_store(+ "u", "a", audit_session_id="s-other"+ )++ asyncio.run(_go())+ assert seen == [("s-other", "t-live")]diff --git a/packages/core/tests/unit/test_chat_route_turn_attribution.py b/packages/core/tests/unit/test_chat_route_turn_attribution.pyindex d8079305..74a463cc 100644--- a/packages/core/tests/unit/test_chat_route_turn_attribution.py+++ b/packages/core/tests/unit/test_chat_route_turn_attribution.py@@ -23,10 +23,12 @@ The contract pinned here:"""from __future__ import annotations+import asynciofrom collections.abc import AsyncIteratorfrom pathlib import Pathfrom typing import Any+import httpximport pytestfrom fastapi import FastAPIfrom fastapi.testclient import TestClient@@ -200,3 +202,90 @@ def test_turn_id_is_generated_when_the_caller_supplies_none() -> None:for fn in (Executive.stream_chat, Executive.stream_chat_with_committee):param = inspect.signature(fn).parameters["turn_id"]assert param.default is None, f"{fn.__name__} must not require turn_id"+++# --------------------------------------------------------------------- #+# Isolation: the binding is held across an async generator+# --------------------------------------------------------------------- #+++def test_concurrent_turns_do_not_cross_contaminate(+ temp_db: Path, patched_deps: None, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Two turns in flight at once must not see each other's identity.++ The binding is now held across an async generator for the whole+ response body, so this pins the property that makes that safe: each+ request's ASGI task owns its own context. A leak here would mean audit+ rows attributed to the wrong session.+ """+ from openexecutive.audit.context import get_active_ids+ from openexecutive.orchestrator import executive as exec_mod++ # message -> list of (session_id, turn_id) observed on each step+ observed: dict[str, list[tuple[str | None, str | None]]] = {}++ class _Interleaving:+ _THINKING = exec_mod.Executive._THINKING++ def __init__(self, **_k: Any) -> None:+ pass++ async def stream_chat(self, *, user_message: str = "", **_k: Any) -> AsyncIterator[str]:+ for _ in range(5):+ observed.setdefault(user_message, []).append(get_active_ids())+ # Yield control so the two requests genuinely interleave.+ await asyncio.sleep(0)+ yield "x "++ monkeypatch.setattr(exec_mod, "Executive", _Interleaving)++ app = FastAPI()+ app.include_router(chat_route.router)++ async def _go() -> None:+ transport = httpx.ASGITransport(app=app)+ async with httpx.AsyncClient(transport=transport, base_url="http://t") as c:+ await asyncio.gather(+ c.post("/chat", json={"message": "alpha"}),+ c.post("/chat", json={"message": "bravo"}),+ )++ asyncio.run(_go())++ assert set(observed) == {"alpha", "bravo"}, observed+ for msg, ids in observed.items():+ assert len(set(ids)) == 1, f"{msg} saw shifting ids: {ids}"+ assert ids[0][0] is not None and ids[0][1] is not None, f"{msg} unbound: {ids}"+ a = observed["alpha"][0]+ b = observed["bravo"][0]+ assert a != b, f"two concurrent turns shared identity: {a} == {b}"+++def test_binding_is_restored_after_the_response(+ temp_db: Path, patched_deps: None, monkeypatch: pytest.MonkeyPatch+) -> None:+ """No leak to unrelated work on the same task after the turn ends."""+ from openexecutive.audit.context import get_active_ids+ from openexecutive.orchestrator import executive as exec_mod++ class _Tiny:+ _THINKING = exec_mod.Executive._THINKING++ def __init__(self, **_k: Any) -> None:+ pass++ async def stream_chat(self, **_k: Any) -> AsyncIterator[str]:+ yield "done "++ monkeypatch.setattr(exec_mod, "Executive", _Tiny)+ app = FastAPI()+ app.include_router(chat_route.router)++ async def _go() -> tuple[str | None, str | None]:+ transport = httpx.ASGITransport(app=app)+ async with httpx.AsyncClient(transport=transport, base_url="http://t") as c:+ await c.post("/chat", json={"message": "leak check"})+ return get_active_ids()++ assert asyncio.run(_go()) == (None, None)