diff --git a/packages/bub-acp-server/src/bub_acp_server/agent.py b/packages/bub-acp-server/src/bub_acp_server/agent.py index 2bb6524..abab639 100644 --- a/packages/bub-acp-server/src/bub_acp_server/agent.py +++ b/packages/bub-acp-server/src/bub_acp_server/agent.py @@ -5,6 +5,7 @@ import contextlib import json import logging +import os import re import time from collections import deque @@ -953,7 +954,8 @@ def _build_inbound( context: dict[str, str] = {} if model := session.runtime.get("model"): context["_runtime_model"] = model - if reasoning_effort := session.runtime.get(REASONING_EFFORT_CONFIG_ID): + reasoning_effort = session.runtime.get(REASONING_EFFORT_CONFIG_ID) + if reasoning_effort and reasoning_effort != "auto": context["_runtime_reasoning_effort"] = reasoning_effort context["_runtime_workspace"] = str(session.cwd) inbound = ACPInboundMessage( @@ -1408,11 +1410,14 @@ def _load_sessions(self) -> dict[str, ACPSession]: def _save_sessions(self) -> None: self._session_store_path.parent.mkdir(parents=True, exist_ok=True) payload = [session.to_json() for session in self._sessions.values()] - temp_path = self._session_store_path.with_suffix(".json.tmp") + temp_path = self._session_store_path.with_suffix(f".json.{os.getpid()}.tmp") temp_path.write_text( json.dumps(payload, indent=2, sort_keys=True), encoding="utf-8" ) - temp_path.replace(self._session_store_path) + try: + temp_path.replace(self._session_store_path) + except FileNotFoundError: + temp_path.unlink(missing_ok=True) async def _attach_session_history(self, session: ACPSession) -> None: router = self._require_stream_router() diff --git a/packages/bub-acp-server/tests/test_plugin.py b/packages/bub-acp-server/tests/test_plugin.py index 690c62b..e714cd6 100644 --- a/packages/bub-acp-server/tests/test_plugin.py +++ b/packages/bub-acp-server/tests/test_plugin.py @@ -1340,3 +1340,44 @@ async def test_prompt_sends_complete_output_when_stream_has_no_text_chunks() -> assert client.updates[0][1].content.text == "late text" assert len(client.updates) == 1 + + +@pytest.mark.asyncio +async def test_auto_reasoning_effort_is_not_forwarded(tmp_path: Path) -> None: + framework = ConfigFramework() + client = FakeClient() + agent = BubACPAgent(framework) + agent.on_connect(client) + created = await agent.new_session(cwd=str(tmp_path)) + await agent.set_config_option( + config_id="reasoning_effort", + session_id=created.session_id, + value="auto", + ) + + await agent.prompt( + [TextContentBlock(type="text", text="hello")], + session_id=created.session_id, + ) + + assert "_runtime_reasoning_effort" not in framework.messages[0].context + + +def test_save_sessions_tolerates_concurrent_store_rotation( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + agent = BubACPAgent(FakeFramework()) + agent._session_store_path = tmp_path / "acp-sessions.json" + agent._sessions["s1"] = agent_module.ACPSession(session_id="s1", cwd=tmp_path) + + def failing_replace(self: Path, target: Path) -> Path: + raise FileNotFoundError(str(self)) + + monkeypatch.setattr(Path, "replace", failing_replace) + agent._save_sessions() # must not raise + assert not list(tmp_path.glob("*.tmp")) + monkeypatch.undo() + + agent._save_sessions() + data = json.loads((tmp_path / "acp-sessions.json").read_text(encoding="utf-8")) + assert [item["session_id"] for item in data] == ["s1"]