Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(media): saved file names carry no colon, so writes work on Windows
trohitg58854aa4deca
diff --git a/docs-internal/media_transport.md b/docs-internal/media_transport.mdindex 0d964cb9..65c733f0 100644--- a/docs-internal/media_transport.md+++ b/docs-internal/media_transport.md@@ -145,7 +145,7 @@ construction for every node that adopts it.| Function | Use ||---|---|-| `write_audio(payload, *, ctx, stem, ext, …) -> AudioRef` | Atomic write into `<workspace>/audio/`. Filename is `<slug>-<node8>-<rand6>.<ext>`, so retries and repeated runs never collide or overwrite. Probes the file and populates the metadata. |+| `write_audio(payload, *, ctx, stem, ext, …) -> AudioRef` | Atomic write into `<workspace>/audio/`. Filename is `<slug>-<node8>-<rand6>.<ext>`, so retries and repeated runs never collide or overwrite. `<node8>` is the node id's first eight characters through `core.paths.safe_path_component`, because canvas ids carry colons (`7:mobile:1`) and Windows refuses a colon in a file name. Probes the file and populates the metadata. || `resolve_media(ref_or_path, *, ctx \| workspace_dir) -> Path` | Contained absolute path. Absolute inputs are tolerated for back-compat but still contained. || `read_media_bytes(...) -> (filename, bytes)` | `resolve_media` + non-empty and size checks. || `coerce_file_param(value, *, ctx) -> (filename, bytes)` | **The one nodes should call.** Accepts all three shapes the UI can produce. |diff --git a/server/nodes/microsoft/_base.py b/server/nodes/microsoft/_base.pyindex f4ae15b1..a0cf372b 100644--- a/server/nodes/microsoft/_base.py+++ b/server/nodes/microsoft/_base.py@@ -176,6 +176,7 @@ def write_attachment_bytes(payload, *, ctx, filename, mime_type=None):import mimetypesfrom uuid import uuid4+ from core.paths import safe_path_componentfrom nodes.filesystem._backend import atomic_write_bytesfrom services.media.refs import FileReffrom services.media.workspace import _slugify, resolve_media, workspace_file_url, workspace_root@@ -185,14 +186,15 @@ def write_attachment_bytes(payload, *, ctx, filename, mime_type=None):raise NodeUserError("Refusing to write an empty attachment file.")root = workspace_root(ctx)- node_id = str(getattr(ctx, "node_id", "") or "node")+ # Canvas node ids carry colons (``7:mail:1``), which Windows refuses in a file name.+ node = safe_path_component(str(getattr(ctx, "node_id", "") or "node")[:8], "node")workflow_id = getattr(ctx, "workflow_id", None)# Preserve the real extension (documentParser globs on *.pdf); slugify the stem.dot = filename.rfind(".") if filename else -1stem = filename[:dot] if dot > 0 else (filename or "attachment")ext = filename[dot + 1 :].lower() if dot > 0 else ""- safe = f"{_slugify(stem)}-{node_id[:8]}-{uuid4().hex[:6]}"+ safe = f"{_slugify(stem)}-{node}-{uuid4().hex[:6]}"name = f"{safe}.{ext}" if ext else saferel = f"attachments/{name}"diff --git a/server/services/media/workspace.py b/server/services/media/workspace.pyindex 31f6e8a4..0cca4c21 100644--- a/server/services/media/workspace.py+++ b/server/services/media/workspace.py@@ -188,7 +188,10 @@ def _persist(naming, containment and atomicity -- the filename shape``<stem>-<node8>-<rand6>.<ext>`` carries a random suffix precisely soretries and repeated runs never collide or silently overwrite.+ ``<node8>`` is the node id's start made safe for a file name: canvas ids+ carry colons (``7:mobile:1``), which Windows refuses."""+ from core.paths import safe_path_componentfrom nodes.filesystem._backend import atomic_write_bytesfrom services.plugin import NodeUserError@@ -196,8 +199,8 @@ def _persist(raise NodeUserError(empty_message)root = workspace_root(ctx)- node_id = str(getattr(ctx, "node_id", "") or "node")- name = f"{_slugify(stem)}-{node_id[:8]}-{uuid4().hex[:6]}.{ext.lstrip('.')}"+ node = safe_path_component(str(getattr(ctx, "node_id", "") or "node")[:8], "node")+ name = f"{_slugify(stem)}-{node}-{uuid4().hex[:6]}.{ext.lstrip('.')}"rel = f"{subdir}/{name}"target = resolve_media(rel, ctx=ctx)diff --git a/server/tests/media/test_write_media.py b/server/tests/media/test_write_media.pyindex 6e6a8554..f5d8be59 100644--- a/server/tests/media/test_write_media.py+++ b/server/tests/media/test_write_media.py@@ -125,6 +125,18 @@ class TestWriteMedia:with pytest.raises(NodeUserError):write_media(b"", ctx=_ctx(tmp_path), stem="x", ext="bin")+ def test_a_canvas_node_id_puts_no_colon_in_the_file_name(self, tmp_path):+ """Canvas node ids are ``<workflow>:<type>:<n>``, and Windows refuses a+ colon in a file name (WinError 123), so every media write failed there."""+ ctx = _ctx(tmp_path, node_id="7:mobile:1")+ for ref in (+ write_media(b"png", ctx=ctx, stem="screenshot", ext="png", kind="image"),+ write_audio(_wav_bytes(), ctx=ctx, stem="clip", ext="wav"),+ ):+ assert ":" not in ref.path+ assert "-7_mobile-" in ref.filename+ assert resolve_media(ref, ctx=ctx).is_file()+def test_sha256_matches_payload(self, tmp_path):import hashlibdiff --git a/server/tests/nodes/test_microsoft.py b/server/tests/nodes/test_microsoft.pyindex 14b9d27f..9fbb7157 100644--- a/server/tests/nodes/test_microsoft.py+++ b/server/tests/nodes/test_microsoft.py@@ -586,6 +586,18 @@ class TestMsMailListAttachments:class TestMsMailDownloadAttachments:+ def test_a_canvas_node_id_puts_no_colon_in_the_file_name(self, tmp_path):+ """Windows refuses a colon in a file name, and canvas node ids have two."""+ from pathlib import Path+ from types import SimpleNamespace++ from nodes.microsoft._base import write_attachment_bytes++ ctx = SimpleNamespace(node_id="7:msMail:1", workflow_id="wf-1", workspace_dir=str(tmp_path), raw={})+ ref, written = write_attachment_bytes(b"%PDF-1.4", ctx=ctx, filename="quote.pdf")+ assert ":" not in ref.path and ref.path.startswith("attachments/quote-7_msMail-")+ assert Path(written).read_bytes() == b"%PDF-1.4"+@respx.mockasync def test_download_writes_file_and_returns_path(self, harness, tmp_path):pdf_bytes = b"%PDF-1.4 fake pdf body"