Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b08f441
fix(backup): run workspace zip export/import off the event loop
jayfeng7 Oct 4, 2026
1f6f81d
test(backup): verify archive worker threads and preserve streaming reads
jayfeng7 Oct 4, 2026
58fb175
Merge upstream develop and retain archive changelog entry
jayfeng7 Oct 4, 2026
e71bc38
merge: sync archive offload with develop
jayfeng7 Oct 5, 2026
ecd68b1
Merge remote-tracking branch 'origin/develop' into fix/workspace-arch…
jayfeng7 Oct 5, 2026
447c1eb
merge: sync archive offload with develop
jayfeng77 Oct 9, 2026
dd0bf3d
Merge branch 'develop' into fix/workspace-archive-offload
jayfeng7 Oct 13, 2026
e00bf1f
Merge remote-tracking branch 'origin/develop' into fix/workspace-arch…
jayfeng7 Oct 14, 2026
adba557
Merge remote-tracking branch 'origin/develop' into fix/workspace-arch…
jayfeng7 Oct 14, 2026
b32e879
Merge remote-tracking branch 'origin/develop' into fix/workspace-arch…
jayfeng7 Oct 14, 2026
7840567
Merge remote-tracking branch 'origin/develop' into fix/workspace-arch…
jayfeng7 Oct 15, 2026
882d55d
Merge origin/develop into fix/workspace-archive-offload
jayfeng7 Oct 19, 2026
12f316d
merge develop: keep released 1.0.3b1 entries, return unreleased fixes…
jayfeng7 Oct 25, 2026
768f67a
merge develop into fix/workspace-archive-offload
jayfeng7 Oct 27, 2026
dbc28d4
merge develop into fix/workspace-archive-offload
jayfeng7 Oct 30, 2026
1e77054
Merge develop into fix/workspace-archive-offload
jayfeng7 Nov 11, 2026
3176485
Merge develop into fix/workspace-archive-offload
jayfeng7 Nov 11, 2026
7a535b5
merge develop and preserve workspace archive regression coverage
jayfeng7 Nov 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

## [Unreleased]

### 修复
- 工作区 ZIP 导出压缩与封包、导入解压及 replace 清理移到工作线程,避免阻塞 API、聊天流与 WebSocket 共用的事件循环;导出逐个读取并压缩文件。

## [1.0.3b7] - 2026-11-17

### 新增
Expand Down
11 changes: 8 additions & 3 deletions src/octop/infra/backup/workspace_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,15 @@ def _read_entry_bytes(workspace: BackendWorkspace, rel: str) -> bytes | None:
async def export_workspace_zip(workspace: BackendWorkspace) -> bytes:
"""Pack workspace files into a zip archive."""
paths = await _list_file_paths(workspace)
return await asyncio.to_thread(_pack_zip, workspace, paths)


def _pack_zip(workspace: BackendWorkspace, paths: list[str]) -> bytes:
"""Read and compress one file at a time outside the event loop."""
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", compression=zipfile.ZIP_DEFLATED) as zf:
for path in paths:
blob = await asyncio.to_thread(_read_entry_bytes, workspace, path)
blob = _read_entry_bytes(workspace, path)
if blob is None:
continue
zf.writestr(path.lstrip("/"), blob)
Expand All @@ -240,11 +245,11 @@ async def import_workspace_zip(
local_workspace_dir: Path | None = None,
) -> dict[str, int | str | list[str]]:
"""Import a zip archive into the workspace."""
entries = _iter_zip_entries(data)
entries = await asyncio.to_thread(_iter_zip_entries, data)
warnings: list[str] = []

if mode == "replace" and local_workspace_dir is not None:
_clear_local_workspace(local_workspace_dir)
await asyncio.to_thread(_clear_local_workspace, local_workspace_dir)
elif mode == "replace":
warnings.append(
"replace mode cleared only the local harness workspace; remote-only files may remain"
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/backup/test_workspace_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import io
import threading
import zipfile
from pathlib import Path
from types import SimpleNamespace
Expand All @@ -13,6 +14,7 @@
from octop_harness.backends import resolve_backend
from octop_harness.backends.workspace import BackendWorkspace

from octop.infra.backup import workspace_archive
from octop.infra.backup.workspace_archive import export_workspace_zip, import_workspace_zip


Expand Down Expand Up @@ -266,3 +268,49 @@ async def test_import_skips_octop_builtin_skills(tmp_path: Path) -> None:
assert not (ws / ".octop" / "_builtin_skills").exists()
assert result["imported"] == 1
assert any("_builtin_skills" in warning for warning in result["warnings"])


@pytest.mark.asyncio
@pytest.mark.parametrize(
"helper_name", ["_pack_zip", "_iter_zip_entries", "_clear_local_workspace"]
)
async def test_workspace_archive_offloads_blocking_zip_work(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, helper_name: str
) -> None:
"""Archive packing, extraction and replace cleanup execute outside the event loop."""
payload = b"hello" * 4096
(tmp_path / "hello.txt").write_bytes(payload)
backend = LocalShellBackend(root_dir=str(tmp_path), virtual_mode=False)
workspace = BackendWorkspace(backend, tmp_path)
loop_thread = threading.get_ident()
worker_threads: list[int] = []
original = getattr(workspace_archive, helper_name)

def record_thread(*args: Any) -> Any:
worker_threads.append(threading.get_ident())
return original(*args)

monkeypatch.setattr(workspace_archive, helper_name, record_thread)

if helper_name == "_pack_zip":
blob = await export_workspace_zip(workspace)
with zipfile.ZipFile(io.BytesIO(blob)) as archive:
assert archive.read("hello.txt") == payload
else:
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w", compression=zipfile.ZIP_DEFLATED) as archive:
archive.writestr("hello.txt", payload)
(tmp_path / "hello.txt").unlink()
(tmp_path / "obsolete.txt").write_bytes(b"old")
(tmp_path / ".env").write_bytes(b"keep")
mode = "replace" if helper_name == "_clear_local_workspace" else "merge"
result = await import_workspace_zip(
workspace, buffer.getvalue(), mode=mode, local_workspace_dir=tmp_path
)
assert result["imported"] == 1
assert (tmp_path / "hello.txt").read_bytes() == payload
assert (tmp_path / ".env").read_bytes() == b"keep"
assert (tmp_path / "obsolete.txt").exists() is (mode == "merge")

assert len(worker_threads) == 1
assert worker_threads[0] != loop_thread, f"{helper_name} ran on the event-loop thread"
Loading