diff --git a/src/takopi/telegram/loop.py b/src/takopi/telegram/loop.py index cc3b817..6f4baed 100644 --- a/src/takopi/telegram/loop.py +++ b/src/takopi/telegram/loop.py @@ -777,12 +777,14 @@ async def run_main_loop( if command_id == "new": if topic_store is not None and topic_key is not None: tg.start_soon( - _handle_new_command, - cfg, - msg, - topic_store, - resolved_topics_scope, - topics_chat_ids, + partial( + _handle_new_command, + cfg, + msg, + topic_store, + resolved_scope=resolved_topics_scope, + scope_chat_ids=topics_chat_ids, + ) ) continue if chat_session_store is not None: @@ -796,12 +798,14 @@ async def run_main_loop( continue if topic_store is not None: tg.start_soon( - _handle_new_command, - cfg, - msg, - topic_store, - resolved_topics_scope, - topics_chat_ids, + partial( + _handle_new_command, + cfg, + msg, + topic_store, + resolved_scope=resolved_topics_scope, + scope_chat_ids=topics_chat_ids, + ) ) continue if command_id is not None and _dispatch_builtin_command( diff --git a/tests/test_telegram_bridge.py b/tests/test_telegram_bridge.py index e2468a1..eb84931 100644 --- a/tests/test_telegram_bridge.py +++ b/tests/test_telegram_bridge.py @@ -2064,6 +2064,56 @@ async def test_run_main_loop_new_clears_chat_sessions(tmp_path: Path) -> None: assert await store2.get_session_resume(123, None, CODEX_ENGINE) is None +@pytest.mark.anyio +async def test_run_main_loop_new_clears_topic_sessions(tmp_path: Path) -> None: + state_path = tmp_path / "takopi.toml" + store = TopicStateStore(resolve_state_path(state_path)) + await store.set_session_resume( + 123, 77, ResumeToken(engine=CODEX_ENGINE, value="resume-1") + ) + + transport = _FakeTransport() + bot = _FakeBot() + runner = ScriptRunner([Return(answer="ok")], engine=CODEX_ENGINE) + exec_cfg = ExecBridgeConfig( + transport=transport, + presenter=MarkdownPresenter(), + final_notify=True, + ) + runtime = TransportRuntime( + router=_make_router(runner), + projects=_empty_projects(), + config_path=state_path, + ) + cfg = TelegramBridgeConfig( + bot=bot, + runtime=runtime, + chat_id=123, + startup_msg="", + exec_cfg=exec_cfg, + topics=TelegramTopicsSettings(enabled=True, scope="main"), + ) + + async def poller(_cfg: TelegramBridgeConfig): + yield TelegramIncomingMessage( + transport="telegram", + chat_id=123, + message_id=1, + text="/new", + reply_to_message_id=None, + reply_to_text=None, + sender_id=123, + thread_id=77, + chat_type="supergroup", + ) + + with anyio.fail_after(2): + await run_main_loop(cfg, poller) + + store2 = TopicStateStore(resolve_state_path(state_path)) + assert await store2.get_session_resume(123, 77, CODEX_ENGINE) is None + + @pytest.mark.anyio async def test_run_main_loop_replies_in_same_thread() -> None: transport = _FakeTransport()