Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(logging): never restore a log context token in a context that did not make it (#428)
wojtyniakAQ362bd6a70085
diff --git a/core/switch_core/bridges/agent/api/operations.py b/core/switch_core/bridges/agent/api/operations.pyindex a20e5eaba..77f83e412 100644--- a/core/switch_core/bridges/agent/api/operations.py+++ b/core/switch_core/bridges/agent/api/operations.py@@ -26,11 +26,7 @@ from fastapi import APIRouter, Depends, Header, HTTPExceptionfrom switch_core.bridges.agent.auth import get_agent_from_scopefrom switch_core.bridges.agent.dependencies import get_protocolfrom switch_core.bridges.agent.operations import all_operations, get_operation-from switch_core.bridges.agent.operations.callctx import (- CallContext,- reset_call_context,- set_call_context,-)+from switch_core.bridges.agent.operations.callctx import CallContext, call_contextfrom switch_core.bridges.agent.protocol.connections import UnknownConnectionErrorfrom switch_core.bridges.agent.protocol.service import ProtocolServicefrom switch_core.db.models import Agent@@ -103,14 +99,11 @@ async def call_operation(if missing:raise BadArgumentsError(f"{operation} requires: {', '.join(missing)}")- token = set_call_context(CallContext(agent_id=agent_id, session_key=connection_id))- try:+ with call_context(CallContext(agent_id=agent_id, session_key=connection_id)):result = fn(**call_args)if inspect.isawaitable(result):result = await resultreturn result- finally:- reset_call_context(token)# ── HTTP router ──────────────────────────────────────────────────────────────diff --git a/core/switch_core/bridges/agent/auth.py b/core/switch_core/bridges/agent/auth.pyindex 9e1cc1373..fac0bb763 100644--- a/core/switch_core/bridges/agent/auth.py+++ b/core/switch_core/bridges/agent/auth.py@@ -20,7 +20,7 @@ from switch_core.db.tenant_lookup import (tenant_of_agent_oauth_client,tenant_of_api_key,)-from switch_core.logging_context import bind_log_context, unbind_log_context+from switch_core.logging_context import log_contextfrom switch_core.tenant_context import tenant_scopelogger = logging.getLogger(__name__)@@ -162,12 +162,11 @@ class BearerAuthMiddleware:# path resolves straight to an Agent with no ApiKey row, so it# falls back to the agent's own tenant.tenant_id = api_key.tenant_id if api_key is not None else agent.tenant_id- with tenant_scope(tenant_id):- log_token = bind_log_context(agent_id=agent.id, tenant_id=tenant_id)- try:- await self.app(scope, receive, send)- finally:- unbind_log_context(log_token)+ with (+ tenant_scope(tenant_id),+ log_context(agent_id=agent.id, tenant_id=tenant_id),+ ):+ await self.app(scope, receive, send)return# Registration token: pass through (handler validates again). MCP rejects.@@ -184,12 +183,11 @@ class BearerAuthMiddleware:# tenant zero by fallback and make that wrong answer permanent and# self-confirming — so bind the token's own tenant here, exactly# as an agent key does.- with tenant_scope(api_key.tenant_id):- log_token = bind_log_context(tenant_id=api_key.tenant_id)- try:- await self.app(scope, receive, send)- finally:- unbind_log_context(log_token)+ with (+ tenant_scope(api_key.tenant_id),+ log_context(tenant_id=api_key.tenant_id),+ ):+ await self.app(scope, receive, send)returnresponse = Response("Invalid credentials", status_code=401)diff --git a/core/switch_core/bridges/agent/mcp/server.py b/core/switch_core/bridges/agent/mcp/server.pyindex f66b5dd79..1c4b640fc 100644--- a/core/switch_core/bridges/agent/mcp/server.py+++ b/core/switch_core/bridges/agent/mcp/server.py@@ -23,11 +23,7 @@ from starlette.types import ASGIAppfrom switch_core.bridges.agent.auth import BearerAuthMiddleware, OIDCTokenValidatorfrom switch_core.bridges.agent.operations import all_operations-from switch_core.bridges.agent.operations.callctx import (- CallContext,- reset_call_context,- set_call_context,-)+from switch_core.bridges.agent.operations.callctx import CallContext, call_contextfrom switch_core.bridges.agent.operations.context import init_operations_protocolif TYPE_CHECKING:@@ -64,18 +60,15 @@ class CallContextMiddleware(Middleware):raise ValueError("No agent_id in request — authentication failed")fastmcp_context = context.fastmcp_context- token = set_call_context(+ with call_context(CallContext(agent_id=agent_id,session_key=(fastmcp_context.session_id if fastmcp_context is not None else None),)- )- try:+ ):return await call_next(context)- finally:- reset_call_context(token)mcp = FastMCP("Switch")diff --git a/core/switch_core/bridges/agent/operations/callctx.py b/core/switch_core/bridges/agent/operations/callctx.pyindex e7c07cb90..4089cdfaf 100644--- a/core/switch_core/bridges/agent/operations/callctx.py+++ b/core/switch_core/bridges/agent/operations/callctx.py@@ -13,12 +13,15 @@ room binding", and operations only ever compare them for equality.from __future__ import annotations+from collections.abc import Iterator+from contextlib import contextmanagerfrom contextvars import ContextVar, Tokenfrom dataclasses import dataclassfrom switch_core.logging_context import (LogContext,bind_log_context,+ restore_unless_finalising,unbind_log_context,)@@ -62,3 +65,19 @@ def reset_call_context(token: _CallContextToken) -> None:def current_call_context() -> CallContext | None:return _current.get()+++@contextmanager+def call_context(context: CallContext) -> Iterator[None]:+ """Bind `context` for the block and restore both tokens on the way out.++ The one caller-facing way to use `set_call_context`/`reset_call_context`:+ a front door binds the caller for an operation call that it does not+ control the internals of, so the call may suspend and never resume — the+ caller's coroutine dropped and finalised by the garbage collector rather+ than completed. `restore_unless_finalising` skips the reset pair in that+ case instead of raising trying to reset a token from the wrong context.+ """+ token = set_call_context(context)+ with restore_unless_finalising(lambda: reset_call_context(token)):+ yielddiff --git a/core/switch_core/gateway/auth.py b/core/switch_core/gateway/auth.pyindex 4aa2dfa1c..61e14a694 100644--- a/core/switch_core/gateway/auth.py+++ b/core/switch_core/gateway/auth.py@@ -22,7 +22,7 @@ from switch_core.gateway.dependencies import (get_session_factory,get_user_store,)-from switch_core.logging_context import bind_log_context, unbind_log_context+from switch_core.logging_context import log_contextfrom switch_core.tenant_context import tenant_scopelogger = logging.getLogger(__name__)@@ -223,16 +223,12 @@ async def get_current_user(tenant_id = await _resolve_tenant_id(session_factory, user_store, user_id)- with tenant_scope(tenant_id):- log_token = bind_log_context(user_id=user_id, tenant_id=tenant_id)- try:- user = await user_store.get(session, user_id)- if user is None:- # Deleted between the two reads; rare, and still not a 500.- raise HTTPException(status_code=401, detail="User not found")- yield user- finally:- unbind_log_context(log_token)+ with tenant_scope(tenant_id), log_context(user_id=user_id, tenant_id=tenant_id):+ user = await user_store.get(session, user_id)+ if user is None:+ # Deleted between the two reads; rare, and still not a 500.+ raise HTTPException(status_code=401, detail="User not found")+ yield userasync def require_admin(diff --git a/core/switch_core/logging_context.py b/core/switch_core/logging_context.pyindex 958f30429..abef5180b 100644--- a/core/switch_core/logging_context.py+++ b/core/switch_core/logging_context.py@@ -30,7 +30,7 @@ row the way the two bindings could.from __future__ import annotationsimport logging-from collections.abc import Iterator+from collections.abc import Callable, Iteratorfrom contextlib import contextmanagerfrom contextvars import ContextVar, Tokenfrom dataclasses import dataclass, replace@@ -80,12 +80,41 @@ def unbind_log_context(token: Token[LogContext]) -> None:@contextmanager-def log_context(**fields: str | None) -> Iterator[None]:- token = bind_log_context(**fields)+def restore_unless_finalising(restore: Callable[[], None]) -> Iterator[None]:+ """Run `restore` on the way out of the block, except when it is unwound by+ `GeneratorExit`.++ `GeneratorExit` here means the frame is being *finalised* rather than+ resumed — a coroutine dropped while suspended and later closed by the+ garbage collector — and the collector runs that close in whatever context+ it happens to be in, not the one that entered the block. A `restore` built+ on a `contextvars.Token.reset` refuses to cross contexts, correctly:+ running it there would stamp this scope's state onto an unrelated one.+ There is also nothing left to restore, since the context the token+ belongs to is unreachable, which is why the frame is being finalised at+ all.++ Shared by every scope built on a context-variable token — `log_context`+ below, and the call context in `bridges/agent/operations/callctx.py`,+ which restores both a call token and a log token together — so the+ finalisation check is written once rather than duplicated per token type.+ """+ finalising = Falsetry:yield+ except GeneratorExit:+ finalising = True+ raisefinally:- unbind_log_context(token)+ if not finalising:+ restore()+++@contextmanager+def log_context(**fields: str | None) -> Iterator[None]:+ token = bind_log_context(**fields)+ with restore_unless_finalising(lambda: unbind_log_context(token)):+ yieldclass LogContextFilter(logging.Filter):diff --git a/core/switch_core/request_context.py b/core/switch_core/request_context.pyindex 65709cd4c..84a197698 100644--- a/core/switch_core/request_context.py+++ b/core/switch_core/request_context.py@@ -17,7 +17,7 @@ from uuid import uuid4from starlette.types import ASGIApp, Receive, Scope, Send-from switch_core.logging_context import bind_log_context, unbind_log_context+from switch_core.logging_context import log_context_REQUEST_ID_HEADER = b"x-request-id"# An id from a caller is untrusted input that ends up in every log line for the@@ -34,11 +34,8 @@ class RequestContextMiddleware:await self.app(scope, receive, send)return- token = bind_log_context(request_id=_request_id(scope))- try:+ with log_context(request_id=_request_id(scope)):await self.app(scope, receive, send)- finally:- unbind_log_context(token)def _request_id(scope: Scope) -> str:diff --git a/core/tests/switch_core/bridges/agent/api/test_operations.py b/core/tests/switch_core/bridges/agent/api/test_operations.pyindex 289c3df5d..06964de98 100644--- a/core/tests/switch_core/bridges/agent/api/test_operations.py+++ b/core/tests/switch_core/bridges/agent/api/test_operations.py@@ -7,6 +7,10 @@ adds an operation and these fail, that is the point.from __future__ import annotations+import contextvars+from collections.abc import Coroutine, Generator+from typing import Any+import pytestfrom switch_core.bridges.agent.api.operations import (@@ -24,6 +28,7 @@ from switch_core.bridges.agent.operations.callctx import (reset_call_context,set_call_context,)+from switch_core.logging_context import current_log_contextAGENT = "agent-1"@@ -163,6 +168,63 @@ def test_session_key_is_none_when_bound_to_nothing() -> None:assert op_context.session_key() is None+class _Suspend:+ """An await that suspends once, so a coroutine can be left mid-scope with+ no event loop in sight."""++ def __await__(self) -> Generator[None, None, None]:+ yield+++def _drive_then_finalise_elsewhere(coro: Coroutine[Any, Any, None]) -> None:+ """Enter the scope inside its own context, then close from this one —+ what the garbage collector does to a coroutine dropped while suspended."""+ contextvars.copy_context().run(coro.send, None)+ coro.close()+++async def test_the_call_context_survives_a_dropped_operation_call() -> None:+ """A caller that suspends inside an operation and is then dropped — the+ coroutine finalised by the garbage collector rather than resumed — must+ not raise trying to restore a token from the wrong context.+ """++ async def hanging_op(room_id: str) -> str:+ await _Suspend()+ return "unreached"++ from switch_core.bridges.agent.operations import registry++ registry._REGISTRY["hanging_op"] = registry.Operation(+ name="hanging_op",+ fn=hanging_op,+ description="hanging",+ input_schema=registry._input_schema(hanging_op),+ )+ try:++ async def body() -> None:+ await call_operation(+ operation="hanging_op",+ arguments={"room_id": "room-1"},+ agent_id=AGENT,+ connection_id="conn-9",+ )++ assert current_call_context() is None+ _drive_then_finalise_elsewhere(body())+ assert current_call_context() is None, (+ "finalising a dropped operation call leaked its call context "+ "into the collector's context"+ )+ assert current_log_context().agent_id is None, (+ "finalising a dropped operation call leaked its log context "+ "into the collector's context"+ )+ finally:+ registry._REGISTRY.pop("hanging_op", None)++def test_the_registry_refuses_duplicate_operation_names() -> None:from switch_core.bridges.agent.operations import registrydiff --git a/core/tests/switch_core/bridges/agent/mcp/test_call_context_middleware.py b/core/tests/switch_core/bridges/agent/mcp/test_call_context_middleware.pynew file mode 100644index 000000000..aacf0798f--- /dev/null+++ b/core/tests/switch_core/bridges/agent/mcp/test_call_context_middleware.py@@ -0,0 +1,67 @@+"""CallContextMiddleware's call-context restore.++Same finalisation edge as `test_callctx.py`, exercised through the actual+front door: an MCP tool call that suspends and is then dropped and finalised+by the garbage collector, rather than resumed, must not raise trying to reset+a token from the wrong context.+"""++from __future__ import annotations++import contextvars+from collections.abc import Coroutine, Generator+from typing import Any++from fastmcp.server.middleware import MiddlewareContext++from switch_core.bridges.agent.mcp import server as mcp_server+from switch_core.bridges.agent.operations.callctx import current_call_context+from switch_core.logging_context import current_log_context++AGENT = "agent-1"+++class _Suspend:+ """An await that suspends once, so a coroutine can be left mid-scope with+ no event loop in sight."""++ def __await__(self) -> Generator[None, None, None]:+ yield+++def _drive_then_finalise_elsewhere(coro: Coroutine[Any, Any, None]) -> None:+ """Enter the scope inside its own context, then close from this one —+ what the garbage collector does to a coroutine dropped while suspended."""+ contextvars.copy_context().run(coro.send, None)+ coro.close()+++class _FakeRequest:+ def __init__(self, agent_id: str) -> None:+ self.scope = {"agent_id": agent_id}+++def test_the_call_context_survives_being_closed_from_another_context(+ monkeypatch: Any,+) -> None:+ monkeypatch.setattr(mcp_server, "get_http_request", lambda: _FakeRequest(AGENT))++ middleware = mcp_server.CallContextMiddleware()+ context: MiddlewareContext = MiddlewareContext(message=None, fastmcp_context=None)++ async def call_next(_context: MiddlewareContext) -> None:+ await _Suspend()++ async def body() -> None:+ await middleware.on_call_tool(context, call_next)++ assert current_call_context() is None+ _drive_then_finalise_elsewhere(body())+ assert current_call_context() is None, (+ "finalising a dropped tool call leaked its call context into the "+ "collector's context"+ )+ assert current_log_context().agent_id is None, (+ "finalising a dropped tool call leaked its log context into the "+ "collector's context"+ )diff --git a/core/tests/switch_core/bridges/agent/operations/test_callctx.py b/core/tests/switch_core/bridges/agent/operations/test_callctx.pynew file mode 100644index 000000000..b5dbae7aa--- /dev/null+++ b/core/tests/switch_core/bridges/agent/operations/test_callctx.py@@ -0,0 +1,55 @@+"""switch_core.bridges.agent.operations.callctx: the finalisation edge of+`call_context`.++Mirrors `test_logging_context.py`'s case for `log_context`, but for the call+context: it restores a call token *and* a log token together, so both must be+skipped consistently when the block is unwound by `GeneratorExit` rather than+resumed.+"""++from __future__ import annotations++import contextvars+from collections.abc import Coroutine, Generator+from typing import Any++from switch_core.bridges.agent.operations.callctx import (+ CallContext,+ call_context,+ current_call_context,+)+from switch_core.logging_context import current_log_context++AGENT = "agent-1"+++class _Suspend:+ """An await that suspends once, so a coroutine can be left mid-scope with+ no event loop in sight."""++ def __await__(self) -> Generator[None, None, None]:+ yield+++def _drive_then_finalise_elsewhere(coro: Coroutine[Any, Any, None]) -> None:+ """Enter the scope inside its own context, then close from this one —+ what the garbage collector does to a coroutine dropped while suspended."""+ contextvars.copy_context().run(coro.send, None)+ coro.close()+++def test_call_context_survives_being_closed_from_another_context() -> None:+ async def body() -> None:+ with call_context(CallContext(agent_id=AGENT, session_key="c1")):+ await _Suspend()++ assert current_call_context() is None+ _drive_then_finalise_elsewhere(body())+ assert current_call_context() is None, (+ "finalising a dropped coroutine leaked its call context into the "+ "collector's context"+ )+ assert current_log_context().agent_id is None, (+ "finalising a dropped coroutine leaked its log context into the "+ "collector's context"+ )diff --git a/core/tests/switch_core/bridges/agent/test_bearer_auth_log_context.py b/core/tests/switch_core/bridges/agent/test_bearer_auth_log_context.pynew file mode 100644index 000000000..60ec39a5c--- /dev/null+++ b/core/tests/switch_core/bridges/agent/test_bearer_auth_log_context.py@@ -0,0 +1,115 @@+"""BearerAuthMiddleware's log-context restore on the agent-authenticated path.++Mirrors `test_logging_context.py`'s finalisation case for the raw+`bind_log_context`/`unbind_log_context` pair the middleware used to carry in a+`try`/`finally`: a downstream app coroutine dropped while suspended and+finalised by the garbage collector runs its `finally` in whatever context the+collector happens to be in, not the one the token was bound in.++The agent token is resolved from the in-memory `ApiKeyCache` rather than a+database lookup, so the only suspension point left in the middleware's+coroutine is the downstream app — the one this test needs to control.+"""++from __future__ import annotations++import contextvars+import hashlib+from collections.abc import Coroutine, Generator+from typing import Any++from switch_core.bridges.agent.api_key_cache import ApiKeyCache+from switch_core.bridges.agent.auth import BearerAuthMiddleware+from switch_core.db.models import Agent, ApiKey+from switch_core.db.stores.agent_store import AgentStore+from switch_core.db.stores.api_key_store import ApiKeyStore+from switch_core.logging_context import current_log_context, log_context++TOKEN = "agent-token"+AGENT_ID = "agent-1"+++class _Suspend:+ """An await that suspends once, so a coroutine can be left mid-scope with+ no event loop in sight."""++ def __await__(self) -> Generator[None, None, None]:+ yield+++def _drive_then_finalise_elsewhere(coro: Coroutine[Any, Any, None]) -> None:+ """Enter the scope inside its own context, then close from this one —+ what the garbage collector does to a coroutine dropped while suspended."""+ contextvars.copy_context().run(coro.send, None)+ coro.close()+++def _middleware() -> tuple[BearerAuthMiddleware, dict]:+ called: dict[str, Any] = {}++ async def _app(scope: Any, receive: Any, send: Any) -> None:+ called["scope"] = scope+ await _Suspend()++ cache = ApiKeyCache(ttl_seconds=5, max_entries=8)+ cache.put(+ hashlib.sha256(TOKEN.encode()).hexdigest(),+ ApiKey(+ id="key-1",+ user_id="user-1",+ key_hash="unused",+ encrypted_key="enc",+ label="k",+ type="agent",+ ),+ Agent(+ id=AGENT_ID,+ name="agent",+ description="",+ agent_type="always_on",+ connector_type="claude_code",+ integration_profile={},+ client_id="client-1",+ api_key_id="key-1",+ ),+ )++ async def _never(*args: Any, **kwargs: Any) -> Any:+ raise AssertionError("the cache hit should have made a DB lookup unnecessary")++ mw = BearerAuthMiddleware(+ _app,+ agent_store=AgentStore(),+ api_key_store=ApiKeyStore(),+ api_key_cache=cache,+ session_factory=_never, # type: ignore[arg-type]+ )+ return mw, called+++async def test_the_agent_log_context_survives_being_closed_from_another_context() -> (+ None+):+ mw, called = _middleware()++ async def receive() -> dict:+ return {"type": "http.request"}++ async def send(message: dict) -> None:+ return None++ scope = {+ "type": "http",+ "path": "/agents",+ "headers": [(b"authorization", f"Bearer {TOKEN}".encode())],+ }++ with log_context(request_id="caller"):+ _drive_then_finalise_elsewhere(mw(scope, receive, send))+ assert called["scope"]["agent_id"] == AGENT_ID, (+ "middleware never reached the app"+ )+ assert current_log_context().request_id == "caller", (+ "finalising a dropped downstream coroutine leaked the agent's log "+ "context into the collector's context"+ )diff --git a/core/tests/switch_core/test_logging_context.py b/core/tests/switch_core/test_logging_context.pynew file mode 100644index 000000000..4326d600e--- /dev/null+++ b/core/tests/switch_core/test_logging_context.py@@ -0,0 +1,42 @@+"""switch_core.logging_context: the finalisation edge of `log_context`.++Covers the case `test_logging_config.py` cannot: what happens when the block+is never resumed to completion, but the coroutine holding it is dropped and+later closed by the garbage collector instead.+"""++from __future__ import annotations++import contextvars+from collections.abc import Coroutine, Generator+from typing import Any++from switch_core.logging_context import current_log_context, log_context+++class _Suspend:+ """An await that suspends once, so a coroutine can be left mid-scope with+ no event loop in sight."""++ def __await__(self) -> Generator[None, None, None]:+ yield+++def _drive_then_finalise_elsewhere(coro: Coroutine[Any, Any, None]) -> None:+ """Enter the scope inside its own context, then close from this one —+ what the garbage collector does to a coroutine dropped while suspended."""+ contextvars.copy_context().run(coro.send, None)+ coro.close()+++def test_log_context_survives_being_closed_from_another_context() -> None:+ async def body() -> None:+ with log_context(request_id="req-1"):+ await _Suspend()++ with log_context(request_id="caller"):+ _drive_then_finalise_elsewhere(body())+ assert current_log_context().request_id == "caller", (+ "finalising a dropped coroutine leaked its log context into the "+ "collector's context"+ )diff --git a/core/tests/switch_core/test_request_context.py b/core/tests/switch_core/test_request_context.pyindex ce6ef9ab0..36591f993 100644--- a/core/tests/switch_core/test_request_context.py+++ b/core/tests/switch_core/test_request_context.py@@ -1,7 +1,11 @@+import contextvars+from collections.abc import Coroutine, Generator+from typing import Any+import pytestfrom starlette.types import Receive, Scope, Send-from switch_core.logging_context import LogContext, current_log_context+from switch_core.logging_context import LogContext, current_log_context, log_contextfrom switch_core.request_context import RequestContextMiddleware@@ -90,6 +94,40 @@ async def test_the_context_is_unbound_when_the_app_raises() -> None:assert current_log_context().request_id is None+class _Suspend:+ """An await that suspends once, so a coroutine can be left mid-scope with+ no event loop in sight."""++ def __await__(self) -> Generator[None, None, None]:+ yield+++def _drive_then_finalise_elsewhere(coro: Coroutine[Any, Any, None]) -> None:+ """Enter the scope inside its own context, then close from this one —+ what the garbage collector does to a coroutine dropped while suspended."""+ contextvars.copy_context().run(coro.send, None)+ coro.close()+++async def test_the_context_survives_being_closed_from_another_context() -> None:+ """A downstream app dropped while suspended and finalised by the garbage+ collector must not raise trying to restore the request-id token from the+ wrong context."""++ class _Hanging:+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:+ await _Suspend()++ mw = RequestContextMiddleware(_Hanging())++ with log_context(request_id="caller"):+ _drive_then_finalise_elsewhere(mw(_scope([]), _receive, _send))+ assert current_log_context().request_id == "caller", (+ "finalising a dropped downstream coroutine leaked its log "+ "context into the collector's context"+ )++async def test_lifespan_traffic_is_passed_through_untouched() -> None:app = _Recorder()