Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(webui): serialize manifest reads with atomic replacement
chengyongru9d7cc0204cf6
diff --git a/nanobot/webui/transcript.py b/nanobot/webui/transcript.pyindex 3a5b61d88..c1f9754f6 100644--- a/nanobot/webui/transcript.py+++ b/nanobot/webui/transcript.py@@ -597,9 +597,7 @@ def _read_segment_manifest_entries(*,stats: TranscriptReplayStats | None = None,) -> list[dict[str, Any]]:- entries = _load_segment_manifest_entries(session_key)- if entries is not None:- return entries+ # Windows requires readers to close the manifest before atomic replacement.with _manifest_rebuild_lock(session_key):entries = _load_segment_manifest_entries(session_key)if entries is not None:diff --git a/tests/utils/test_webui_transcript.py b/tests/utils/test_webui_transcript.pyindex 0e00ed33c..f060fa158 100644--- a/tests/utils/test_webui_transcript.py+++ b/tests/utils/test_webui_transcript.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport jsonfrom concurrent.futures import ThreadPoolExecutor+from contextlib import contextmanagerfrom pathlib import Pathfrom threading import Event, Lock@@ -579,26 +580,63 @@ def test_manifest_repair_is_single_flight(tmp_path, monkeypatch) -> None:manifest = webui_transcript_segments_dir(key) / "manifest.json"manifest.write_text("{not json", encoding="utf-8")original = transcript_module._rebuild_segment_manifest+ original_read_text = Path.read_text+ original_replace = transcript_module.os.replace+ manifest_lock = transcript_module._manifest_rebuild_lock(key)rebuild_started = Event()- release_rebuild = Event()+ second_attempt = Event()+ manifest_open = Event()+ release_read = Event()calls = 0calls_lock = Lock()+ @contextmanager+ def track_lock(_session_key):+ if rebuild_started.is_set():+ second_attempt.set()+ with manifest_lock:+ yield++ def hold_manifest_read(path, *args, **kwargs):+ if path != manifest or not rebuild_started.is_set() or release_read.is_set():+ return original_read_text(path, *args, **kwargs)+ with path.open(encoding="utf-8") as handle:+ manifest_open.set()+ second_attempt.set()+ try:+ assert release_read.wait(5)+ return handle.read()+ finally:+ manifest_open.clear()++ def replace_manifest(source, destination):+ if destination != manifest:+ return original_replace(source, destination)+ try:+ # Windows cannot replace a file held open by a concurrent reader.+ if manifest_open.is_set():+ raise PermissionError("manifest is open for reading")+ return original_replace(source, destination)+ finally:+ release_read.set()+def slow_rebuild(*args, **kwargs):nonlocal callswith calls_lock:calls += 1rebuild_started.set()- assert release_rebuild.wait(2)+ assert second_attempt.wait(5)return original(*args, **kwargs)+ monkeypatch.setattr(transcript_module, "_manifest_rebuild_lock", track_lock)+ monkeypatch.setattr(Path, "read_text", hold_manifest_read)+ monkeypatch.setattr(transcript_module.os, "replace", replace_manifest)monkeypatch.setattr(transcript_module, "_rebuild_segment_manifest", slow_rebuild)with ThreadPoolExecutor(max_workers=2) as executor:first = executor.submit(transcript_module._read_segment_manifest_entries, key)- assert rebuild_started.wait(2)+ assert rebuild_started.wait(5)second = executor.submit(transcript_module._read_segment_manifest_entries, key)- release_rebuild.set()- assert first.result(timeout=2) == second.result(timeout=2)+ assert first.result(timeout=5) == second.result(timeout=5)assert calls == 1