fix(gateway): deny wake authority for caller-supplied conversation_history

#98619 review follow-up: an explicit session_id plus non-empty
caller-supplied conversation_history skipped the SessionDB load yet was
still marked wake-capable ('not previous_response_id' alone). Such a
turn is authoritative on the caller snapshot and never consumes the
SessionDB delivery row a detached completion persists, so wake is now
fixed before the history load can overwrite the variable and denied on
the same default-deny contract as previous_response_id continuations.

Adds an HTTP regression proving the binder receives wake_capable=""
and _conversation_history_for_session stays uncalled on that branch.
This commit is contained in:
liuhao1024
2026-09-07 12:03:19 +08:00
committed by Teknium
parent 0efb525420
commit 71818aaf52
2 changed files with 63 additions and 4 deletions
+11 -4
View File
@@ -319,7 +319,9 @@ class _RunLaunch:
user_message: str
conversation_history: List[Dict[str, str]]
# #98619: only continuation paths that reload session history may grant wake authority —
# a previous_response_id continuation consumes its ResponseStore snapshot instead.
# a previous_response_id continuation consumes its ResponseStore snapshot instead, and a
# caller-supplied conversation_history is authoritative for the turn; neither consumes a
# SessionDB delivery row, so both stay default-denied.
wake_capable: bool
agent_kwargs: dict # ``_create_agent`` keyword arguments (prompt, model overrides, route, room policy)
request_profile: Any
@@ -455,7 +457,10 @@ async def _handle_runs(self, request: "web.Request", *, _api_server) -> "web.Res
# same-key run's context (#98619). previous_response_id continuations keep their
# ResponseStore snapshot as history (they cannot consume a SessionDB delivery row and are
# accordingly denied wake capability in _run_agent_sync); the fresh run_id fallback has
# nothing persisted to load yet.
# nothing persisted to load yet. Wake authority is fixed here, before the load can
# overwrite ``conversation_history``: a caller-supplied history is authoritative for this
# turn, never consumes the SessionDB delivery row, and is denied on the same contract.
wake_capable = not previous_response_id and not conversation_history
if not conversation_history and selected_session_id and not previous_response_id:
conversation_history = await self._conversation_history_for_session(str(selected_session_id))
q = self._run_streams[run_id] = asyncio.Queue()
@@ -476,7 +481,7 @@ async def _handle_runs(self, request: "web.Request", *, _api_server) -> "web.Res
self._run_idempotency_ids.add(run_id)
launch = _RunLaunch(
self, run_id, q, session_id, gateway_session_key, _declared_selected, user_message,
conversation_history, not previous_response_id,
conversation_history, wake_capable,
agent_kwargs=dict(
ephemeral_system_prompt=instructions, session_id=session_id, gateway_session_key=gateway_session_key,
route=route, room_dispatch=room_dispatch, room_execution_policy=room_execution_policy,
@@ -526,7 +531,9 @@ def _run_agent_sync(self, run: _RunLaunch, agent, approval_notify, *, _api_serve
# SessionDB in _handle_runs), or the run_id fallback the client can post back
# as body.session_id. A previous_response_id continuation consumes its
# ResponseStore snapshot instead and can never see a SessionDB delivery row,
# so it stays default-denied until a merge contract exists for that chain.
# so it stays default-denied until a merge contract exists for that chain;
# likewise a caller-supplied conversation_history is authoritative for the
# turn and never reads the delivery row, so it is denied the same way.
wake_capable="1" if run.wake_capable else "")
if session_tokens:
resets.append((session_tokens, clear_session_vars))
+52
View File
@@ -1627,6 +1627,58 @@ class TestRunIdempotency:
await asyncio.sleep(0.05)
assert bind.call_args.kwargs["wake_capable"] == "1"
@pytest.mark.asyncio
async def test_caller_supplied_history_with_session_id_is_not_wake_capable(
self, adapter, tmp_path
):
"""#98619: an explicit session_id plus non-empty caller-supplied
conversation_history skips the SessionDB load, so the run's continuation
path never consumes a SessionDB delivery row (async completion persists
to SessionDB only) and must NOT be granted wake authority — same
default-deny contract as previous_response_id continuations."""
_use_idempotency_db(adapter, tmp_path / "idem.db")
mock_db = MagicMock()
mock_db.resolve_resume_session_id.side_effect = lambda sid: sid
adapter._session_db = mock_db
caller_history = [
{"role": "user", "content": "caller turn"},
{"role": "assistant", "content": "caller reply"},
]
bind = MagicMock(return_value=[])
history_load = AsyncMock(return_value=[])
app = _create_runs_app(adapter)
async with TestClient(TestServer(app)) as cli:
with (
patch.object(adapter, "_bind_api_server_session", new=bind),
patch.object(adapter, "_conversation_history_for_session", new=history_load),
patch.object(adapter, "_create_agent") as create,
):
agent = MagicMock()
agent.run_conversation.return_value = {"final_response": "done"}
agent.session_prompt_tokens = agent.session_completion_tokens = (
agent.session_total_tokens
) = 0
create.return_value = agent
response = await cli.post(
"/v1/runs",
json={
"input": "hello",
"session_id": "client-held-session",
"conversation_history": caller_history,
},
)
assert response.status == 202
for _ in range(40):
if bind.called:
break
await asyncio.sleep(0.05)
assert bind.call_args.kwargs["wake_capable"] == ""
history_load.assert_not_called()
assert (
agent.run_conversation.call_args.kwargs["conversation_history"]
== caller_history
)
@pytest.mark.asyncio
async def test_compressed_parent_delivery_row_lands_on_live_tip(
self, adapter, tmp_path