"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(agent): the compaction summary call inherits the bound LLM session (#1416)
Haozhe Wufd6c7c7c0885
diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.pyindex 42ff667b6..59793bf5a 100644--- a/agent/src/agent/loop.py+++ b/agent/src/agent/loop.py@@ -14,6 +14,7 @@ Tool execution:from __future__ import annotationsimport concurrent.futures+import contextvarsimport copyimport jsonimport logging@@ -3187,8 +3188,12 @@ class AgentLoop:_compact_error.append(exc)if _compact_timeout > 0:+ # A new thread starts with an empty context, so the session+ # bound by run() (x-opencode-session, #1416) would not reach+ # this call: run it inside a copy of the caller's context.worker = threading.Thread(- target=_run_compact_summary,+ target=contextvars.copy_context().run,+ args=(_run_compact_summary,),name="compact-summary",daemon=True,)diff --git a/agent/tests/test_compaction_call_identity.py b/agent/tests/test_compaction_call_identity.pyindex 4d3bf970c..3c75cacd8 100644--- a/agent/tests/test_compaction_call_identity.py+++ b/agent/tests/test_compaction_call_identity.py@@ -279,3 +279,37 @@ def test_auto_compact_reopens_head_but_preserves_tail_gate(result = h.call({"statement": "balance"})assert json.loads(result["content"])["skipped"] is Trueassert len(h.tool.calls) == 3+++def test_auto_compact_summary_call_sees_the_bound_llm_session(harness, monkeypatch):+ """The compaction thread inherits the run's session binding (#1416).++ ``AgentLoop.run()`` binds the session id in a ``ContextVar``. A fresh+ thread starts with an empty context, so the summary call used to go out+ unbound and OpenCode Go saw a second conversation id for the same session.+ """+ from src.providers.session_context import (+ bind_llm_session_id,+ current_llm_session_id,+ reset_llm_session_id,+ )++ h = harness+ h.call({"statement": "income"})+ h.call({"statement": "balance"})+ seen: list[str] = []++ def chat(*args, **kwargs):+ seen.append(current_llm_session_id())+ return SimpleNamespace(content="summary")++ monkeypatch.setattr(h.agent.llm, "chat", chat)+ monkeypatch.setattr("src.agent.loop._llm_timeout_seconds", lambda: 1)+ token = bind_llm_session_id("vibe-session-42")+ try:+ h.agent._auto_compact(h.messages, h.run_dir, h.trace)+ finally:+ reset_llm_session_id(token)++ assert seen, "the compaction made no summary call"+ assert seen == ["vibe-session-42"] * len(seen)