DialogueRetrievalAgent reuses history filename for independent default-session runs in the same second

Author: Sumire-no-kaiCreated Sep 29, 2026Updated Sep 29, 2026

Summary

DialogueRetrievalAgent gives independent calls the same generated history filename when they use the default session_id='', share a workspace, and start within the same second. The second call overwrites the file, while document retrieval can reuse the first call's cached content for that path. The second model request then receives history from the first conversation instead of its own.

Steps to reproduce

This uses two agent instances, the public run() API, and an offline recording model. A fixed clock makes the same-second condition deterministic. Set the workspace environment variable before importing qwen_agent:

python
import datetime
import os
import tempfile
import types
from unittest.mock import patch

os.environ['QWEN_AGENT_DEFAULT_WORKSPACE'] = tempfile.mkdtemp()

from qwen_agent.agents import DialogueRetrievalAgent
from qwen_agent.agents import dialogue_retrieval_agent as dialogue_module
from qwen_agent.llm import BaseChatModel
from qwen_agent.llm.schema import Message


class RecordingModel(BaseChatModel):
    def __init__(self):
        super().__init__({'model': 'offline-recording-model'})
        self.knowledge_prompt = ''

    def _chat_no_stream(self, messages, generate_cfg):
        system = '\n'.join(str(m.content) for m in messages if m.role == 'system')
        if '# Knowledge Base' in system:
            self.knowledge_prompt = system
            answer = 'recorded'
        else:
            answer = '{"keywords_en": ["stored", "fact"], "keywords_zh": []}'
        return [Message(role='assistant', content=answer)]

    def _chat_stream(self, messages, delta_stream, generate_cfg):
        yield self._chat_no_stream(messages, generate_cfg)

    def _chat_with_functions(self, messages, functions, stream, delta_stream, generate_cfg, lang):
        raise AssertionError('No function tools are configured')


class FixedClock(datetime.datetime):
    @classmethod
    def now(cls, tz=None):
        return cls(2026, 9, 29, 12, 0, 0)


def conversation(marker):
    return [
        {'role': 'user', 'content': f'My stored fact is {marker}.'},
        {'role': 'assistant', 'content': 'Acknowledged.'},
        {'role': 'user', 'content': 'What is my stored fact?'},
    ]


model_a, model_b = RecordingModel(), RecordingModel()
agent_a = DialogueRetrievalAgent(llm=model_a)
agent_b = DialogueRetrievalAgent(llm=model_b)

with patch.object(dialogue_module, 'datetime', types.SimpleNamespace(datetime=FixedClock)):
    list(agent_a.run(conversation('A_ONLY')))
    list(agent_b.run(conversation('B_ONLY')))

print('B prompt contains A:', 'A_ONLY' in model_b.knowledge_prompt)
print('B prompt contains B:', 'B_ONLY' in model_b.knowledge_prompt)

Observed:

B prompt contains A: True
B prompt contains B: False

Both calls write dialogue_history__20260929_120000.txt. After the second call, the file itself contains B_ONLY, but its model input contains A_ONLY. The unmodified document parser logs a cache read for the second call. Running the same calls with distinct session_id values, or with timestamps in different seconds, gives B its own history.

I also reproduced the collision once without replacing the clock: two sequential offline calls started at 14:01:09.505651 and 14:01:09.635532 on the same machine, and B again received A's history. This establishes the real same-second trigger without measuring how often it occurs with network models.

Expected behavior

Independent conversations should retain their own supplied history when using the default API. The repository's examples/long_dialogue.py calls bot.run(messages) without a session_id.

Cause and scope

qwen_agent/agents/dialogue_retrieval_agent.py builds the path from session_id and %Y%m%d_%H%M%S, then writes the history to that path. A path used by multiple distinct histories also becomes the document cache identity. The observed wrong content is present in the model input before inference; it does not depend on model answer quality.

Tested against Qwen-Agent main 31a4d36d123688581a9e9744427272b33ce940e0 (package version 0.0.34), Python 3.12.14, with a local recording model and synthetic content. I did not test a hosted service or measure real-model collision frequency.

This differs from #414, which asks how to refresh the cache after intentionally updating a document at the same URL. Here the generated names of two independent conversation histories collide.