@@ -3081,14 +3081,15 @@ def length_aware_mkdir(self: Path, mode: int = 0o777, parents: bool = False, exi
30813081
30823082 monkeypatch .setattr (Path , "mkdir" , length_aware_mkdir )
30833083 else :
3084- # Grow the parent until it is just inside the directory limit, then add a file name
3085- # that pushes the absolute path past the file limit, so only the open fails.
3084+ # Grow the parent until it is just inside the directory limit, then size the file
3085+ # name from the current length so the absolute path is guaranteed to reach the
3086+ # file limit wherever ``tmp_path`` happens to live.
30863087 segments : list [str ] = []
30873088 current = len (str (tmp_path ))
30883089 while current + 1 + 34 <= 247 :
30893090 segments .append ("d" * 34 )
30903091 current += 1 + 34
3091- key = "/" .join ([* segments , "f" * 20 ])
3092+ key = "/" .join ([* segments , "f" * max ( 1 , 260 - current - 1 ) ])
30923093 monkeypatch .setattr (os , "open" , raise_too_long )
30933094
30943095 with pytest .raises (OSError ) as exc_info :
@@ -3132,3 +3133,69 @@ def raise_missing(*_args: object, **_kwargs: object) -> None:
31323133 await store .write ("nested/file.txt" , "x" )
31333134
31343135 assert exc_info .value is missing
3136+
3137+
3138+ async def test_filesystem_store_measures_long_paths_in_utf16_code_units (
3139+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch
3140+ ) -> None :
3141+ """The Windows limit is compared in UTF-16 code units, not Python code points.
3142+
3143+ A supplementary character such as an emoji is one code point but two UTF-16 units.
3144+ A path one code point short of 260 can therefore already have reached the Windows
3145+ limit, and must still be reported as too long.
3146+ """
3147+ import errno
3148+
3149+ monkeypatch .setattr ("agent_framework._harness._file_access._WINDOWS_PATH_LIMITS_APPLY" , True )
3150+ store = FileSystemAgentFileStore (str (tmp_path ))
3151+ root = str (store .root_path )
3152+ key = "f" * (257 - len (root )) + "\U0001f600 "
3153+ target = store .root_path / key
3154+ # 259 code points, 260 UTF-16 units: only the Windows length has reached the limit.
3155+ assert len (str (target )) == 259
3156+ assert len (str (target ).encode ("utf-16-le" )) // 2 == 260
3157+
3158+ too_long = FileNotFoundError (errno .ENOENT , "The system cannot find the path specified" )
3159+
3160+ def raise_too_long (* _args : object , ** _kwargs : object ) -> None :
3161+ raise too_long
3162+
3163+ monkeypatch .setattr (os , "open" , raise_too_long )
3164+
3165+ with pytest .raises (OSError ) as exc_info :
3166+ await store .write (key , "x" )
3167+
3168+ assert not isinstance (exc_info .value , FileNotFoundError )
3169+ assert exc_info .value .errno == errno .ENAMETOOLONG
3170+ assert exc_info .value .__cause__ is too_long
3171+
3172+
3173+ @pytest .mark .parametrize ("error_type" , [FileExistsError , PermissionError ])
3174+ async def test_filesystem_store_preserves_non_missing_errors_on_long_paths (
3175+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch , error_type : type [OSError ]
3176+ ) -> None :
3177+ """Collisions and permission failures on an over-long path keep their own type.
3178+
3179+ Only the legacy "missing path" symptom is rewritten. With long-path support enabled
3180+ Windows reports an existing file as ``FileExistsError`` and a refusal as
3181+ ``PermissionError``; relabeling either as a length problem would hide the real cause.
3182+ """
3183+ import errno
3184+
3185+ monkeypatch .setattr ("agent_framework._harness._file_access._WINDOWS_PATH_LIMITS_APPLY" , True )
3186+ store = FileSystemAgentFileStore (str (tmp_path ))
3187+ key = "f" * (260 - len (str (store .root_path )) - 1 )
3188+ error_number = errno .EEXIST if error_type is FileExistsError else errno .EACCES
3189+ error = error_type (error_number , "original failure" )
3190+ assert len (str (store .root_path / key )) == 260
3191+
3192+ def raise_error (* _args : object , ** _kwargs : object ) -> None :
3193+ raise error
3194+
3195+ monkeypatch .setattr (os , "open" , raise_error )
3196+
3197+ with pytest .raises (OSError ) as exc_info :
3198+ await store .write (key , "x" )
3199+
3200+ assert exc_info .value is error
3201+ assert exc_info .value .errno == error_number
0 commit comments