@@ -80,9 +80,12 @@ def _get_session_store(request: Request) -> SessionStore:
8080
8181
8282async def _get_session_for_tenant (
83- store : SessionStore , session_id : UUID , tenant : TenantContext
83+ request : Request ,
84+ session_id : UUID ,
85+ tenant : TenantContext ,
8486) -> Session :
85- """Fetch a session and verify it belongs to the tenant's org."""
87+ """Fetch a session and verify it belongs to the tenant's org and this agent."""
88+ store = _get_session_store (request )
8689 try :
8790 session = await store .get_session (session_id )
8891 except SessionNotFoundError :
@@ -91,7 +94,8 @@ async def _get_session_for_tenant(
9194 detail = f"Session { session_id } not found." ,
9295 )
9396
94- if session .org_id != tenant .org_id :
97+ agent_id = request .app .state .settings .agent_id
98+ if session .org_id != tenant .org_id or session .agent_id != agent_id :
9599 raise HTTPException (
96100 status_code = status .HTTP_404_NOT_FOUND ,
97101 detail = f"Session { session_id } not found." ,
@@ -170,7 +174,7 @@ async def send_message(
170174) -> SendMessageResponse :
171175 """Send a user message to a session, triggering agent processing."""
172176 store = _get_session_store (request )
173- session = await _get_session_for_tenant (store , session_id , tenant )
177+ session = await _get_session_for_tenant (request , session_id , tenant )
174178
175179 if session .status not in ("active" , "idle" , "failed" , "paused" ):
176180 raise HTTPException (
@@ -226,8 +230,7 @@ async def confirm_disclosure(
226230 enforcement is enabled. Typically called by the frontend after
227231 showing the AI disclosure notice to the user.
228232 """
229- store = _get_session_store (request )
230- await _get_session_for_tenant (store , session_id , tenant )
233+ await _get_session_for_tenant (request , session_id , tenant )
231234
232235 governance = getattr (request .app .state , "governance_gate" , None )
233236 if governance is not None :
@@ -241,8 +244,7 @@ async def get_session(
241244 tenant : TenantContext = Depends (get_current_tenant ),
242245) -> Session :
243246 """Retrieve metadata for a single session."""
244- store = _get_session_store (request )
245- return await _get_session_for_tenant (store , session_id , tenant )
247+ return await _get_session_for_tenant (request , session_id , tenant )
246248
247249
248250@router .get ("/sessions" , response_model = ListSessionsResponse )
@@ -252,8 +254,9 @@ async def list_sessions(
252254 limit : int = 50 ,
253255 offset : int = 0 ,
254256) -> ListSessionsResponse :
255- """List the authenticated user's sessions (paginated , newest first) ."""
257+ """List the authenticated user's sessions for this agent , newest first."""
256258 store = _get_session_store (request )
259+ settings = request .app .state .settings
257260
258261 if limit < 1 :
259262 limit = 1
@@ -265,6 +268,7 @@ async def list_sessions(
265268 sessions = await store .list_sessions (
266269 org_id = tenant .org_id ,
267270 user_id = tenant .user_id ,
271+ agent_id = settings .agent_id ,
268272 limit = limit ,
269273 offset = offset ,
270274 )
@@ -285,7 +289,7 @@ async def pause_session(
285289) -> Session :
286290 """Pause an active session."""
287291 store = _get_session_store (request )
288- session = await _get_session_for_tenant (store , session_id , tenant )
292+ session = await _get_session_for_tenant (request , session_id , tenant )
289293
290294 if session .status not in ("active" , "processing" , "paused" ):
291295 raise HTTPException (
@@ -319,7 +323,7 @@ async def resume_session(
319323) -> Session :
320324 """Resume a paused session."""
321325 store = _get_session_store (request )
322- session = await _get_session_for_tenant (store , session_id , tenant )
326+ session = await _get_session_for_tenant (request , session_id , tenant )
323327
324328 if session .status != "paused" :
325329 raise HTTPException (
@@ -348,7 +352,7 @@ async def delete_session(
348352) -> None :
349353 """Archive (soft-delete) a session and delete its workspace storage."""
350354 store = _get_session_store (request )
351- await _get_session_for_tenant (store , session_id , tenant )
355+ await _get_session_for_tenant (request , session_id , tenant )
352356
353357 await store .update_session_status (session_id , "archived" )
354358
0 commit comments