Skip to content

Commit 7dcf9cb

Browse files
committed
Python: Drop a stale service thread id from a hosted session on load
A conversation that began before storage was disabled persisted a session naming the service thread that already holds its transcript. Core resumes that thread ahead of anything in the run options, so the model still saw the transcript twice, and the storage check then failed the turn even though the client had honoured store=False. Because a failed turn is not saved, the same stale session loaded again on every later turn. Clearing service_session_id where the transient history buffer is popped heals those conversations on their first post-upgrade turn, and makes the check exact: an id present afterwards can only have been set by this run.
1 parent c9b8aba commit 7dcf9cb

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,13 @@ async def _handle_inner_agent(
625625
try:
626626
if self._uses_hosted_responses_history:
627627
session.state.pop(_HOSTED_RESPONSES_HISTORY_SOURCE_ID, None)
628+
if not self._allow_stored_output_enabled:
629+
# A service session id on a loaded session points at a thread that already holds
630+
# this conversation, written before storage was turned off. Core resumes it ahead
631+
# of anything in the options, so it would duplicate the transcript the platform
632+
# already supplies. Dropping it also makes the check in `finally` exact: an id
633+
# present by then can only have been set by this run.
634+
session.service_session_id = None
628635

629636
input_items = await context.get_input_items()
630637
input_messages = await _items_to_messages(input_items, approval_storage=approval_storage)
@@ -677,10 +684,11 @@ async def _handle_inner_agent(
677684
if self._uses_hosted_responses_history:
678685
session.state.pop(_HOSTED_RESPONSES_HISTORY_SOURCE_ID, None)
679686

680-
# A service session id on the session means the agent's chat client kept the turn
681-
# despite `store=False`, so a second record of this conversation now exists that
682-
# nothing here reconciles. Leave the session unsaved so later turns do not resume onto
683-
# it, and report it: a container configured this way is a server fault, not a bad request.
687+
# The session started this run without a service session id, so one now means the
688+
# agent's chat client kept the turn despite `store=False`, and a second record of this
689+
# conversation exists that nothing here reconciles. Leave the session unsaved so later
690+
# turns do not resume onto it, and report it: a container configured this way is a
691+
# server fault, not a bad request.
684692
stored_output_violation = (
685693
self._uses_hosted_responses_history
686694
and not self._allow_stored_output_enabled

‎python/packages/foundry_hosting/tests/test_responses.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,41 @@ async def test_responses_history_is_not_duplicated_when_the_client_stores_servic
809809
assert stored is not None
810810
assert stored.service_session_id is None
811811

812+
async def test_session_carrying_a_pre_fix_service_thread_id_heals_instead_of_failing(self) -> None:
813+
"""A conversation started before storage was disabled continues cleanly after an upgrade.
814+
815+
Its persisted session still names the service thread that holds the whole transcript, and
816+
core resumes that thread ahead of anything in the options. Left in place it duplicates the
817+
turn and trips the storage violation, and since a failed turn is never re-saved the same
818+
stale session would load again on every later turn.
819+
"""
820+
client = _ServiceStorageRecordingClient()
821+
agent = Agent(client=client, name="Upgraded Agent")
822+
store = SessionStore()
823+
server = _make_server(agent, session_store=store)
824+
825+
first = await _post(server, input_text="first")
826+
# What a pre-fix version left behind: the service thread that already holds turn 1.
827+
upgraded = await store.get(first.json()["id"])
828+
assert upgraded is not None
829+
upgraded.service_session_id = "svc-thread-1"
830+
await store.set(first.json()["id"], upgraded)
831+
832+
second = await _post(server, input_text="second", previous_response_id=first.json()["id"])
833+
834+
assert second.status_code == 200
835+
assert second.json()["status"] == "completed"
836+
# The stale thread is neither resumed nor mistaken for a client that stored the turn.
837+
assert client.conversation_ids == [None, None]
838+
assert [[message.text for message in call] for call in client.calls] == [
839+
["first"],
840+
["first", "recorded", "second"],
841+
]
842+
843+
stored = await store.get(second.json()["id"])
844+
assert stored is not None
845+
assert stored.service_session_id is None
846+
812847
async def test_client_that_stores_despite_disabled_storage_fails_and_leaves_session_unsaved(
813848
self,
814849
caplog: pytest.LogCaptureFixture,

0 commit comments

Comments
 (0)