From 541e3c83c53a3e28fea7bd4c53786e3e0f838229 Mon Sep 17 00:00:00 2001 From: Niels Rogge Date: Mon, 23 Nov 2026 12:27:17 +0000 Subject: [PATCH] Return 410 for session-store calls after a stopped turn Pressing stop revokes the turn capability while the chat still exists, so the worker's next transcript write got 403 and raised the critical session_store_forbidden alert (2026-11-23 10:11). A rejected session-store capability now returns 410 when the token's own capability for that chat, turn and scope was revoked, alongside the existing 404 for a deleted chat. Every other rejection stays 403, and the worker already treats 410 as a non-retryable 4xx. Co-Authored-By: Claude Opus 5.5 --- backend/chat_gateway_app.py | 24 ++++++++---- backend/services/chat_capability_service.py | 21 ++++++++++ backend/tests/chat_gateway/test_turn_fence.py | 17 +++++--- .../chat_sandbox/test_provider_contracts.py | 39 +++++++++++++++++++ docs/autonomous-operations.md | 7 ++-- 5 files changed, 92 insertions(+), 16 deletions(-) diff --git a/backend/chat_gateway_app.py b/backend/chat_gateway_app.py index 11a776079..6dd372804 100644 --- a/backend/chat_gateway_app.py +++ b/backend/chat_gateway_app.py @@ -234,12 +234,24 @@ def _external_turn_fence_headers( ] -def _chat_session_deleted(db: Session, session_id: str) -> bool: +def _session_store_rejection( + db: Session, token: str, session_id: str, turn_id: str +) -> HTTPException: try: chat_session_id = uuid.UUID(session_id) + chat_turn_id = uuid.UUID(turn_id) except ValueError: - return False - return db.get(ChatSession, chat_session_id) is None + return HTTPException(status_code=403, detail="Capability rejected") + if db.get(ChatSession, chat_session_id) is None: + return HTTPException(status_code=404, detail="Chat session not found") + if ChatCapabilityService(db).revoked( + token, + scope="session_store", + chat_session_id=chat_session_id, + turn_id=chat_turn_id, + ): + return HTTPException(status_code=410, detail="Chat turn has ended") + return HTTPException(status_code=403, detail="Capability rejected") def _scope( @@ -262,10 +274,8 @@ def _scope( ) except (CapabilityError, ValueError): db.rollback() - if scope == "session_store" and _chat_session_deleted(db, session_id): - raise HTTPException( - status_code=404, detail="Chat session not found" - ) from None + if scope == "session_store": + raise _session_store_rejection(db, token, session_id, turn_id) from None raise HTTPException(status_code=403, detail="Capability rejected") from None if "external_turn" not in row.scopes: return row diff --git a/backend/services/chat_capability_service.py b/backend/services/chat_capability_service.py index 0683a1181..8eb75dc52 100644 --- a/backend/services/chat_capability_service.py +++ b/backend/services/chat_capability_service.py @@ -231,6 +231,27 @@ def meter_hf_catalog( row.hf_recursive_list_calls += 1 self.db.flush() + def revoked( + self, + token: str, + *, + scope: str, + chat_session_id: uuid.UUID, + turn_id: uuid.UUID, + ) -> bool: + row = self.db.execute( + select(ChatTurnCapability).where( + ChatTurnCapability.token_hash == _digest(token) + ) + ).scalar_one_or_none() + return ( + row is not None + and row.revoked_at is not None + and row.chat_session_id == chat_session_id + and row.turn_id == turn_id + and scope in row.scopes + ) + def revoke(self, capability_id: uuid.UUID) -> bool: row = self.db.get(ChatTurnCapability, capability_id) if row is None or row.revoked_at is not None: diff --git a/backend/tests/chat_gateway/test_turn_fence.py b/backend/tests/chat_gateway/test_turn_fence.py index 3dee09f2a..050fcf388 100644 --- a/backend/tests/chat_gateway/test_turn_fence.py +++ b/backend/tests/chat_gateway/test_turn_fence.py @@ -578,17 +578,22 @@ def _reject_capability(*_args, **_kwargs): @pytest.mark.parametrize( - ("scope", "chat_exists", "status_code"), + ("scope", "chat_exists", "revoked", "status_code"), [ - ("session_store", False, 404), - ("session_store", True, 403), - ("model", False, 403), + ("session_store", False, False, 404), + ("session_store", True, True, 410), + ("session_store", True, False, 403), + ("model", False, False, 403), + ("model", True, True, 403), ], ) -def test_rejected_capability_is_404_only_for_a_deleted_chat_session_store( - monkeypatch, scope, chat_exists, status_code +def test_rejected_session_store_capability_reports_a_deleted_chat_or_ended_turn( + monkeypatch, scope, chat_exists, revoked, status_code ): monkeypatch.setattr(gateway.ChatCapabilityService, "validate", _reject_capability) + monkeypatch.setattr( + gateway.ChatCapabilityService, "revoked", lambda *_args, **_kwargs: revoked + ) db = FenceDB(on_get=lambda *_args: object() if chat_exists else None) with pytest.raises(HTTPException) as error: diff --git a/backend/tests/chat_sandbox/test_provider_contracts.py b/backend/tests/chat_sandbox/test_provider_contracts.py index 8f9b778c2..f960a01b5 100644 --- a/backend/tests/chat_sandbox/test_provider_contracts.py +++ b/backend/tests/chat_sandbox/test_provider_contracts.py @@ -90,6 +90,45 @@ def flush(self): ) +def test_capability_reports_revocation_only_for_its_own_turn_and_scope(): + session_id = uuid.uuid4() + turn_id = uuid.uuid4() + token = "revoked-test-token" + row = ChatTurnCapability( + id=uuid.uuid4(), + token_hash=_digest(token), + chat_session_id=session_id, + turn_id=turn_id, + scopes=["session_store"], + expires_at=datetime.now(UTC) + timedelta(minutes=1), + ) + + class Result: + def scalar_one_or_none(self): + return row + + class DB: + def execute(self, _query): + return Result() + + service = ChatCapabilityService(DB()) + + def revoked(**overrides): + arguments = { + "scope": "session_store", + "chat_session_id": session_id, + "turn_id": turn_id, + } | overrides + return service.revoked(token, **arguments) + + assert not revoked() + row.revoked_at = datetime.now(UTC) + assert revoked() + assert not revoked(scope="model") + assert not revoked(turn_id=uuid.uuid4()) + assert not revoked(chat_session_id=uuid.uuid4()) + + def test_workspace_snapshot_provider_only_snapshots_workspace(): calls: list[tuple[str, int, int]] = [] diff --git a/docs/autonomous-operations.md b/docs/autonomous-operations.md index 9bc240322..896c0c11d 100644 --- a/docs/autonomous-operations.md +++ b/docs/autonomous-operations.md @@ -102,9 +102,10 @@ outage. The latency rule fired in 4 of 1,222 windows. Existing sanitized operational counters still supply persistence and capacity failures. -A session-store call for a chat the user deleted mid-turn returns 404, not 403, -so the `session_store_forbidden` runtime alert counts only real capability -rejections. +A session-store call for a chat the user deleted mid-turn returns 404, and one +whose turn already ended (the user pressed stop, which revokes the turn's +capability) returns 410, not 403. The `session_store_forbidden` runtime alert +therefore counts only real capability rejections. A separately allowlisted synthetic job (`run_chat_synthetic_probe`, `pwc-cron-run-chat-synthetic-probe.timer`) polls its due/trigger state every minute