diff --git a/EvoScientist/cli/commands.py b/EvoScientist/cli/commands.py index b5a0fd1..b870bad 100644 --- a/EvoScientist/cli/commands.py +++ b/EvoScientist/cli/commands.py @@ -306,12 +306,22 @@ async def compact_conversation(agent: Any, thread_id: str | None) -> CompactResu # Generate summary (LLM call) summary = await middleware._acreate_summary(to_summarize) + # Inject thread_id into LangGraph contextvar so _get_thread_id() finds it + # (compact runs outside a runnable context, so get_config() would fail + # and the middleware would generate a random "session_xxx" filename instead + # of reusing the real thread_id). + from langgraph.config import var_child_runnable_config + + _token = var_child_runnable_config.set(config) + # Offload old messages to backend file_path: str | None = None try: file_path = await middleware._aoffload_to_backend(backend, to_summarize) except Exception: pass # non-fatal — proceed without offloaded history + finally: + var_child_runnable_config.reset(_token) summary_msg = middleware._build_new_messages_with_path(summary, file_path)[0]