from pathlib import Path from gfa_core.mas.agent_state_repository import SQLiteAgentStateRepository from gfa_core.mas.persistent_conversation import PersistentConversationStore def _store(tmp_path: Path) -> PersistentConversationStore: repository = SQLiteAgentStateRepository(tmp_path / "agent-state.sqlite3") repository.initialize() return PersistentConversationStore(repository) def test_rebind_to_different_job_creates_empty_conversation_and_preserves_old(tmp_path: Path) -> None: store = _store(tmp_path) old_turn = store.begin_turn( session_id="old-session", job_id="job_old", query="旧 job 的问题", client_history=[], ) new_conversation_id = store.rebind_job( session_id=old_turn.conversation_id, job_id="job_new", ) assert new_conversation_id != old_turn.conversation_id old_conversation = store.repository.get_conversation(old_turn.conversation_id) new_conversation = store.repository.get_conversation(new_conversation_id) assert old_conversation is not None and old_conversation.job_id == "job_old" assert new_conversation is not None and new_conversation.job_id == "job_new" assert [message.content for message in store.repository.list_recent_messages(old_turn.conversation_id)] == ["旧 job 的问题"] assert store.repository.list_recent_messages(new_conversation_id) == () ledger = store.repository.get_goal_ledger(new_conversation_id) assert ledger is not None assert ledger.user_terms == {"active_job_id": "job_new"} def test_rebind_to_same_job_keeps_conversation_identity(tmp_path: Path) -> None: store = _store(tmp_path) turn = store.begin_turn( session_id="same-session", job_id="job_same", query="当前 job 的问题", client_history=[], ) conversation_id = store.rebind_job( session_id=turn.conversation_id, job_id="job_same", ) assert conversation_id == turn.conversation_id