Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(cli): make resource URI segments Windows-safe like memory uris (#4517 precedent) (#4549)

Leoy6271dbbc743c
diff --git a/openviking_cli/utils/uri.py b/openviking_cli/utils/uri.py
index 3421e958d..993f419c4 100644
--- a/openviking_cli/utils/uri.py
+++ b/openviking_cli/utils/uri.py
@@ -10,6 +10,26 @@ viking://<scope>/<path>
import re
from typing import Dict, Optional
+# Win32 reserved device names. A URI segment whose stem matches one of these
+# collides with a Windows device name and causes a silent write failure, so
+# sanitize_segment defuses it with a leading underscore. Mirrors the guard PR
+# #4517 added to the memory path (openviking/session/memory/utils/uri.py).
+_WINDOWS_RESERVED_STEMS = frozenset(
+ {
+ "CON",
+ "PRN",
+ "AUX",
+ "NUL",
+ *(f"COM{index}" for index in range(1, 10)),
+ *(f"LPT{index}" for index in range(1, 10)),
+ }
+)
+
+
+def _windows_reserved_stem(segment: str) -> str:
+ """Return the Win32 device-name stem used for portability checks."""
+ return segment.split(".", 1)[0].rstrip(" .").upper()
+
class VikingURI:
"""
@@ -260,6 +280,11 @@ class VikingURI:
safe = re.sub(r"_+", "_", safe)
# Strip leading/trailing underscores and dots, limit length
safe = safe.strip("_.")[:50]
+ # Defuse Win32 reserved device names (CON/PRN/NUL/AUX/COM1../LPT9..)
+ # so the segment is a valid path component on Windows, mirroring PR
+ # #4517's guard on the memory path.
+ if _windows_reserved_stem(safe) in _WINDOWS_RESERVED_STEMS:
+ safe = f"_{safe}"
return safe or "unnamed"
def __str__(self) -> str:
diff --git a/tests/unit/test_cli_uri_windows_safe.py b/tests/unit/test_cli_uri_windows_safe.py
new file mode 100644
index 000000000..99de94dce
--- /dev/null
+++ b/tests/unit/test_cli_uri_windows_safe.py
@@ -0,0 +1,48 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+"""Tests for Windows-reserved-name safety in resource URI segments.
+
+Mirrors the Win32 device-name guard that PR #4517 added to the memory path
+(``openviking/session/memory/utils/uri.py``) onto the resource path's
+``VikingURI.sanitize_segment``. A segment whose stem is a Win32 reserved
+name (``CON``, ``PRN``, ``NUL``, ``AUX``, ``COM1``..``COM9``,
+``LPT1``..``LPT9``) is prefixed with ``_`` so it does not collide with a
+Windows device name and cause a silent write failure.
+"""
+
+import pytest
+
+from openviking_cli.utils.uri import VikingURI
+
+
+ "reserved",
+ ["CON", "PRN", "NUL", "AUX", "COM1", "LPT1"],
+)
+def test_sanitize_segment_rejects_windows_reserved_names(reserved: str):
+ """A bare Win32 reserved stem is prefixed so it is not a device name."""
+ result = VikingURI.sanitize_segment(reserved)
+ assert result != reserved
+ assert result.startswith("_")
+ # Exact pin: for an already-clean name the only transformation
+ # ``sanitize_segment`` applies is the reserved-stem prefix, so the result
+ # is the input preceded by a single underscore.
+ assert result == f"_{reserved}"
+
+
+def test_sanitize_segment_reserved_with_extension():
+ """A reserved stem carrying an extension is still guarded on the stem.
+
+ ``CON.txt`` -> stem ``CON`` -> ``_CON.txt`` (extension preserved). The
+ guard runs after the existing ``strip("_.")`` step, so the extension
+ survives and only the stem is defused.
+ """
+ assert VikingURI.sanitize_segment("CON.txt") == "_CON.txt"
+
+
+def test_sanitize_segment_preserves_normal_names():
+ """Non-reserved names are unaffected by the added guard."""
+ assert VikingURI.sanitize_segment("my_resource") == "my_resource"
+ # CJK characters are preserved by the existing sanitizer and must not be
+ # touched by the reserved-name guard.
+ assert VikingURI.sanitize_segment("报告") == "报告"