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 def test_pending_proposal_preserves_original_analysis_request(tmp_path: Path) -> None: store = _store(tmp_path) turn = store.begin_turn( session_id="kaks-session", job_id="job_kaks", query="执行kaks分析", client_history=[], ) ledger = store.finish_turn( turn, assistant_message="是否继续?", interaction_kind="independent", action="clarify", proposal={ "proposal_id": "proposal-kaks", "request": "执行kaks分析\n已确认上下文:subfamily_confirm 已完成。", "original_request": "执行kaks分析", "subject": "执行kaks分析", }, ) assert ledger.user_terms["pending_proposal"]["original_request"] == "执行kaks分析"