Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(deepresearch): resolve Windows path and handle bugs

程凯4322cb6277eb
diff --git a/jiuwenswarm/agents/harness/common/tools/deepresearch/rewrite_tools.py b/jiuwenswarm/agents/harness/common/tools/deepresearch/rewrite_tools.py
index 3a38b8a8d4..d650cbc0bb 100644
--- a/jiuwenswarm/agents/harness/common/tools/deepresearch/rewrite_tools.py
+++ b/jiuwenswarm/agents/harness/common/tools/deepresearch/rewrite_tools.py
@@ -411,7 +411,8 @@ async def _deliver_html(
transport = WebSocketGatewayPushTransport()
payload: dict[str, object] = {
"event_type": "chat.file",
- "files": [{"path": str(html_path), "name": html_path.name}],
+ # as_posix keeps the wire format platform-independent.
+ "files": [{"path": html_path.as_posix(), "name": html_path.name}],
}
metadata: dict[str, object] = {}
if html_style_status in {"applied", "fallback"}:
diff --git a/jiuwenswarm/agents/harness/common/tools/deepresearch/todo_progress.py b/jiuwenswarm/agents/harness/common/tools/deepresearch/todo_progress.py
index b7d44300dc..be08598614 100644
--- a/jiuwenswarm/agents/harness/common/tools/deepresearch/todo_progress.py
+++ b/jiuwenswarm/agents/harness/common/tools/deepresearch/todo_progress.py
@@ -125,7 +125,13 @@ def _open_windows_parent(
before = current.lstat()
if stat.S_ISLNK(before.st_mode) or not stat.S_ISDIR(before.st_mode):
raise OSError("unsafe todo parent")
- fd = os.open(current, flags, mode=0o700)
+ try:
+ fd = os.open(current, flags, mode=0o700)
+ except OSError:
+ # os.open cannot open directories on Windows Python; degrade the
+ # fd-based identity check to the lstat identity (best-effort).
+ chain.append((current, (before.st_dev, before.st_ino)))
+ continue
try:
opened = os.fstat(fd)
finally:
diff --git a/jiuwenswarm/agents/harness/common/tools/deepresearch/tools.py b/jiuwenswarm/agents/harness/common/tools/deepresearch/tools.py
index 0a22bb0999..5a38b08cbc 100644
--- a/jiuwenswarm/agents/harness/common/tools/deepresearch/tools.py
+++ b/jiuwenswarm/agents/harness/common/tools/deepresearch/tools.py
@@ -2862,6 +2862,9 @@ def _exclusive_write_at(
os.fsync(stream.fileno())
return metadata.st_dev, metadata.st_ino
except BaseException:
+ # Close before unlinking: on Windows an open handle blocks unlink.
+ with suppress(OSError):
+ os.close(descriptor)
if identity is not None:
try:
current = os.stat(name, dir_fd=root_fd, follow_symlinks=False)
@@ -2876,7 +2879,8 @@ def _exclusive_write_at(
os.unlink(name, dir_fd=root_fd)
raise
finally:
- os.close(descriptor)
+ with suppress(OSError):
+ os.close(descriptor)
def _publish_asset_directory_at(
@@ -3196,6 +3200,9 @@ def _exclusive_write(path: Path, payload: bytes) -> tuple[int, int]:
os.fsync(stream.fileno())
return metadata.st_dev, metadata.st_ino
except BaseException:
+ # Close before unlinking: on Windows an open handle blocks unlink.
+ with suppress(OSError):
+ os.close(descriptor)
if identity is not None:
try:
current = path.lstat()
@@ -3211,7 +3218,8 @@ def _exclusive_write(path: Path, payload: bytes) -> tuple[int, int]:
path.unlink()
raise
finally:
- os.close(descriptor)
+ with suppress(OSError):
+ os.close(descriptor)
async def _generate_report_html(
diff --git a/jiuwenswarm/agents/harness/common/tools/deepresearch_plugin/document_rewrite.py b/jiuwenswarm/agents/harness/common/tools/deepresearch_plugin/document_rewrite.py
index e24da3c648..35560e5d12 100644
--- a/jiuwenswarm/agents/harness/common/tools/deepresearch_plugin/document_rewrite.py
+++ b/jiuwenswarm/agents/harness/common/tools/deepresearch_plugin/document_rewrite.py
@@ -1673,7 +1673,21 @@ def _restore_quarantined_path(
def _cleanup_owned_publication(
path: Path, descriptor: int, *, directory_fd: int | None = None
) -> None:
- """Quarantine a published path before proving it is still our inode."""
+ """Quarantine a published path before proving it is still our inode.
+
+ The descriptor is closed here before the rename: on Windows an open
+ handle blocks rename/unlink (sharing violation), which would leave the
+ sidecar behind. Ownership identity is captured via fstat while the
+ descriptor is still open.
+ """
+ try:
+ owned = os.fstat(descriptor)
+ except OSError:
+ owned = None
+ try:
+ os.close(descriptor)
+ except OSError:
+ pass
quarantine = path.with_name(f".{path.name}.cleanup-{uuid.uuid4().hex}")
try:
if directory_fd is None:
@@ -1687,8 +1701,12 @@ def _cleanup_owned_publication(
)
except OSError:
return
+ if owned is None:
+ _restore_quarantined_path(
+ quarantine, path, directory_fd=directory_fd
+ )
+ return
try:
- owned = os.fstat(descriptor)
moved = (
os.lstat(quarantine)
if directory_fd is None
@@ -1804,7 +1822,6 @@ def _publish_create(
_cleanup_owned_publication(
path, descriptor, directory_fd=directory_fd
)
- os.close(descriptor)
raise
return descriptor
@@ -1928,7 +1945,6 @@ def _publish_child_in_authorized_directory(
provenance_descriptor,
directory_fd=directory_fd,
)
- os.close(provenance_descriptor)
provenance_descriptor = None
logger.error(
"PROVENANCE_DIAG _publish_child provenance create failed type=%s "
@@ -1959,6 +1975,7 @@ def _publish_child_in_authorized_directory(
provenance_descriptor,
directory_fd=directory_fd,
)
+ provenance_descriptor = None
logger.info(
"PROVENANCE_DIAG _publish_child markdown exists attempt=%s path=%s",
attempt,
@@ -1972,13 +1989,13 @@ def _publish_child_in_authorized_directory(
markdown_descriptor,
directory_fd=directory_fd,
)
- os.close(markdown_descriptor)
markdown_descriptor = None
_cleanup_owned_publication(
paths.provenance_path,
provenance_descriptor,
directory_fd=directory_fd,
)
+ provenance_descriptor = None
logger.error(
"PROVENANCE_DIAG _publish_child markdown create failed type=%s "
"errno=%s msg=%s path=%s",
@@ -1995,13 +2012,13 @@ def _publish_child_in_authorized_directory(
markdown_descriptor,
directory_fd=directory_fd,
)
- os.close(markdown_descriptor)
markdown_descriptor = None
_cleanup_owned_publication(
paths.provenance_path,
provenance_descriptor,
directory_fd=directory_fd,
)
+ provenance_descriptor = None
raise
else:
if __debug__ and markdown_descriptor is None:
diff --git a/jiuwenswarm/server/runtime/agent_adapter/workspace_host_init.py b/jiuwenswarm/server/runtime/agent_adapter/workspace_host_init.py
index c53d0fe8ff..6641703dc2 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/workspace_host_init.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/workspace_host_init.py
@@ -89,7 +89,8 @@ def _ensure_workspace_template(
"""进程内预置一份模板目录(同 schema 只建一次)。"""
fp = _dirs_fingerprint(directories)
lang = (language or "cn").strip() or "cn"
- key = f"{lang}:{fp}"
+ # '_' not ':': a colon is invalid in Windows directory names.
+ key = f"{lang}_{fp}"
with _TPL_LOCK:
cached = _TPL_CACHE.get(key)
if cached is not None and (cached / ".workspace").is_file():