Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(agent-core): make the file tools work on Windows (#2465)
Call White16c379bbdd07
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.ymlindex d76a7c86b..c8232799b 100644--- a/.github/workflows/lint.yml+++ b/.github/workflows/lint.yml@@ -686,3 +686,77 @@ jobs:$env:AVIBE_TEST_RIPGREP_ZIPS = Join-Path $env:RUNNER_TEMP "ripgrep-zips"python -c "import json, os, pathlib, urllib.request; target = pathlib.Path(os.environ['AVIBE_TEST_RIPGREP_ZIPS']); target.mkdir(exist_ok=True); archives = json.load(open('vibe/ripgrep_runtime_manifest.json'))['archives']; [urllib.request.urlretrieve(entry['url'], target / entry['name']) for tag, entry in archives.items() if tag.startswith('win32-')]"python -m pytest -q -ra tests/test_ripgrep_runtime.py -k "pinned_windows_zip or six_platform or zip_entry"++ # Vibey's tools on real Windows: what the Linux shards cannot show is how+ # the same Python behaves there (no O_NONBLOCK, no fchmod before 3.13,+ # text-mode descriptors without O_BINARY, st_ctime that means creation time).+ # Both runners use x64 Python: no win_arm64 wheel of cryptography exists, so+ # Avibe runs on Windows on Arm through x64 Python.+ windows-agent:+ name: windows-agent (${{ matrix.os }})+ runs-on: ${{ matrix.os }}+ timeout-minutes: 20+ strategy:+ fail-fast: false+ matrix:+ os: [windows-latest, windows-11-arm]+ steps:+ - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5++ - name: Prepare minimal UI asset placeholder+ run: |+ New-Item -ItemType Directory -Force ui/dist | Out-Null+ Set-Content -Path ui/dist/index.html -Value '<!doctype html><html><body></body></html>'++ - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6+ with:+ python-version: "3.12"+ architecture: x64++ - name: Install pinned uv+ run: |+ python -m pip install --disable-pip-version-check --only-binary=:all: --no-deps uv==0.12.10++ # uv keeps its cache in %LOCALAPPDATA% on Windows; it uses the path this+ # job saves and restores instead, under its own runner's key.+ - name: Point uv at the saved cache+ run: '"UV_CACHE_DIR=$HOME\.cache\uv" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8'++ - uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5+ with:+ path: ~/.cache/uv+ key: ${{ runner.os }}-${{ runner.arch }}-ci-uv-0.12.10-py3.12-${{ hashFiles('pyproject.toml', 'uv.lock') }}++ - name: Install package and test deps+ run: uv pip install --system -e . --group dev++ # A volume without ACLs or named streams, where a replace keeps what it+ # can: FAT32 on a small virtual disk.+ - name: Prepare a FAT volume+ run: |+ $vhd = Join-Path $env:RUNNER_TEMP "fat.vhdx"+ $script = Join-Path $env:RUNNER_TEMP "fat.diskpart"+ @(+ "create vdisk file=`"$vhd`" maximum=64 type=expandable",+ "attach vdisk",+ "create partition primary",+ "format fs=fat32 quick label=AVIBEFAT",+ "assign letter=T"+ ) | Set-Content -Encoding ascii $script+ diskpart /s $script+ if ($LASTEXITCODE -ne 0) { throw "diskpart failed: $LASTEXITCODE" }+ (Get-Volume -DriveLetter T).FileSystem+ "AVIBE_TEST_FAT_DIR=T:\" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8++ # The four CPU-time bounds are set for the Linux shards, which run them;+ # Windows' interpreter is slower, and on Arm it is emulated.+ - name: Verify the file tools+ run: >-+ python -m pytest -q -ra+ -k "not without_walking_it and not bounded_by_lines and not stays_linear and not nfkc_expansion"+ tests/agent_core/tools/test_read.py tests/agent_core/tools/test_write_edit.py+ tests/agent_core/tools/test_file_kinds.py tests/agent_core/tools/test_normalize.py+ tests/agent_core/tools/test_args.py++ - name: Verify Vibey's tool lines+ run: python -m pytest -q -ra tests/test_vibey_agent.py -k relative_tool_pathdiff --git a/core/agent_core/tools/edit.py b/core/agent_core/tools/edit.pyindex d1d6dc0e2..b98c1ef6e 100644--- a/core/agent_core/tools/edit.py+++ b/core/agent_core/tools/edit.py@@ -38,7 +38,7 @@ from core.agent_core.tools.paths import ()from core.agent_core.tools.text import decode_file, encode_file, model_textfrom core.agent_core.tools.truncate import format_size-from core.agent_core.tools.write import FileChanged, FileIdentity, NotReplaceable, write_bytes+from core.agent_core.tools.write import FileChanged, FileIdentity, NotReplaceable, may_write, write_bytes#: Avibe: larger files are refused rather than loaded whole; the display diff stops at a smaller size.MAX_EDIT_BYTES = 10 * 1024 * 1024@@ -192,8 +192,8 @@ def _plan_edits(real = os.path.realpath(absolute)fd = open_regular(real)try:- st = os.fstat(fd)- size = st.st_size+ identity = FileIdentity.of(real, fd)+ size = identity.sizeif size > MAX_EDIT_BYTES:raise _TooLarge(size)raw = read_at_most(fd, MAX_EDIT_BYTES)@@ -206,7 +206,7 @@ def _plan_edits(if lines > MAX_EDIT_LINES:raise _TooManyLines(lines)new_text, before, after, counts, changed = apply_edits(text, edits, path, max_result_chars=MAX_EDIT_BYTES)- return len(raw), encode_file(new_text), before, after, FileIdentity.of(real, st), counts, changed+ return len(raw), encode_file(new_text), before, after, identity, counts, changedclass EditTool:@@ -238,7 +238,7 @@ class EditTool:return error_result(f"Could not edit file: {path}. Error code: EISDIR.")if kind == "other":return error_result(f"Could not edit file: {path}. It is not a regular file.")- if not os.access(absolute, os.R_OK | os.W_OK):+ if not (os.access(absolute, os.R_OK) and may_write(absolute)):return error_result(f"Could not edit file: {path}. Error code: EACCES.")try:size, data, base, new_content, identity, counts, changed = await to_thread_joined(diff --git a/core/agent_core/tools/paths.py b/core/agent_core/tools/paths.pyindex db3d1fc0a..7b213e32c 100644--- a/core/agent_core/tools/paths.py+++ b/core/agent_core/tools/paths.py@@ -225,9 +225,10 @@ def open_regular(path: str, flags: int = os.O_RDONLY) -> int:``O_NONBLOCK`` keeps a FIFO swapped in after an earlier check from blocking the open, and thekind is taken from the descriptor (``fstat``), so sizes and contents come from the same file.- Raises ``NotRegularFile`` or the ``OSError`` of the open.+ Windows has no FIFO to block on, and needs ``O_BINARY``: a text-mode descriptor would drop CRs+ and stop reading at the first 0x1A byte. Raises ``NotRegularFile`` or the ``OSError`` of the open."""- fd = os.open(path, flags | os.O_NONBLOCK | getattr(os, "O_CLOEXEC", 0))+ fd = os.open(path, flags | getattr(os, "O_NONBLOCK", 0) | getattr(os, "O_BINARY", 0) | getattr(os, "O_CLOEXEC", 0))try:mode = os.fstat(fd).st_modeif not stat.S_ISREG(mode):@@ -276,9 +277,10 @@ async def file_mutation_lock(path: str) -> AsyncIterator[None]:"""Serialize ``write`` and ``edit`` on one file, keyed by its canonical path.Different files proceed in parallel. The key follows symlinks, so two names- for one file share a lock.+ for one file share a lock, and so do two spellings of one name on Windows, where names are+ case-insensitive and a file that does not exist yet keeps the spelling it was named with."""- key = os.path.realpath(path)+ key = os.path.normcase(os.path.realpath(path))entry = _locks.get(key)if entry is None:entry = _locks[key] = _PathLock()diff --git a/core/agent_core/tools/windows_files.py b/core/agent_core/tools/windows_files.pynew file mode 100644index 000000000..ada4352c3--- /dev/null+++ b/core/agent_core/tools/windows_files.py@@ -0,0 +1,260 @@+"""What ``write`` and ``edit`` need from Windows files, through ``ctypes``.++POSIX gives them two guarantees by default that Windows gives only on request:++- privacy from the start: a file's permissions are its ACL, which ``os.access`` and ``stat`` modes do+ not see (the mode is only the read-only bit), and a new file has its directory's;+- a change time: ``stat`` reports the creation time as ``st_ctime`` on Python before 3.12.++An existing file is rewritten in place (C-7 tools.md section 3): a replacement would be a new file+object, which keeps none of the old one's owner, security, streams, or links. Importing this module+works everywhere; calling it works only on Windows.+"""++from __future__ import annotations++import ctypes+import os+from typing import Any, Optional++_FILE_WRITE_DATA = 0x0002+_FILE_READ_ATTRIBUTES = 0x0080+_GENERIC_WRITE = 0x40000000+_FILE_SHARE_READ = 0x1+_FILE_SHARE_READ_WRITE = 0x1 | 0x2+_FILE_SHARE_READ_WRITE_DELETE = 0x1 | 0x2 | 0x4+_CREATE_NEW = 1+_OPEN_EXISTING = 3+_FILE_ATTRIBUTE_NORMAL = 0x80+_FILE_ATTRIBUTE_REPARSE_POINT = 0x400+_FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000+_FILE_BASIC_INFO = 0+_FILE_ATTRIBUTE_TAG_INFO = 9+# IsReparseTagNameSurrogate: the reparse point names another file (a symbolic link, a junction).+_REPARSE_TAG_NAME_SURROGATE = 0x20000000+_ERROR_SHARING_VIOLATION = 32+_SDDL_REVISION_1 = 1+# Protected, so nothing is inherited: the owner and SYSTEM alone, as a POSIX 0600 file is the owner's.+_PRIVATE_SDDL = "D:P(A;;FA;;;OW)(A;;FA;;;SY)"+# FILETIME counts 100 ns from 1601-01-01; the Unix epoch is this many of them later.+_UNIX_EPOCH_FILETIME = 116_444_736_000_000_000++_api: Any = None+++def _kernel32() -> Any:+ """kernel32, and the one advapi32 call used here, with their signatures, set up on first use."""+ global _api+ if _api is not None:+ return _api+ from ctypes import wintypes++ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)+ kernel32.CreateFileW.argtypes = [+ wintypes.LPCWSTR,+ wintypes.DWORD,+ wintypes.DWORD,+ ctypes.c_void_p,+ wintypes.DWORD,+ wintypes.DWORD,+ wintypes.HANDLE,+ ]+ kernel32.CreateFileW.restype = wintypes.HANDLE+ kernel32.CloseHandle.argtypes = [wintypes.HANDLE]+ kernel32.CloseHandle.restype = wintypes.BOOL+ kernel32.GetFileInformationByHandleEx.argtypes = [+ wintypes.HANDLE,+ ctypes.c_int,+ ctypes.c_void_p,+ wintypes.DWORD,+ ]+ kernel32.GetFileInformationByHandleEx.restype = wintypes.BOOL+ kernel32.LocalFree.argtypes = [ctypes.c_void_p]+ kernel32.LocalFree.restype = ctypes.c_void_p+ advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)+ advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW.argtypes = [+ wintypes.LPCWSTR,+ wintypes.DWORD,+ ctypes.POINTER(ctypes.c_void_p),+ ctypes.c_void_p,+ ]+ advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW.restype = wintypes.BOOL+ kernel32.advapi32 = advapi32+ _api = kernel32+ return kernel32+++def _invalid(handle: Any) -> bool:+ return handle is None or handle == ctypes.c_void_p(-1).value+++def _error(path: str) -> OSError:+ # With a winerror, OSError takes its errno, and so its subclass, from it.+ error = ctypes.get_last_error()+ return OSError(None, ctypes.FormatError(error), path, error)+++def may_write(path: str) -> bool:+ """Whether this user may write the data of the existing file ``path``, by its ACL and read-only bit.++ The file is opened for writing and closed, never written. Windows checks access before sharing,+ so a sharing violation means the access was granted: another process merely has the file open.+ """+ kernel32 = _kernel32()+ handle = kernel32.CreateFileW(+ path, _FILE_WRITE_DATA, _FILE_SHARE_READ_WRITE_DELETE, None, _OPEN_EXISTING, 0, None+ )+ if _invalid(handle):+ return ctypes.get_last_error() == _ERROR_SHARING_VIOLATION+ kernel32.CloseHandle(handle)+ return True+++def open_for_rewriting(path: str) -> Optional[int]:+ """A binary, write-only descriptor of the existing file ``path`` that asks for writing the data (and reading+ the attributes) alone, and lets no other program write, rename, or delete the file while it is open;+ ``None`` when ``path`` names a link (a symbolic link or a junction) rather than a file.++ The open does not follow a link. Any other reparse point (a OneDrive placeholder, a compressed file) belongs+ to the file itself, which is then opened normally, so the filter behind it does its work.++ ``os.open`` asks for ``GENERIC_WRITE``, which includes reading the file's security, so it fails on a file+ whose ACL grants writing but not ``READ_CONTROL``. Without write sharing, a program that has the file open+ for writing makes this open fail before a byte changes, and none can open it for writing meanwhile.+ """+ import msvcrt++ kernel32 = _kernel32()++ def open_file(flags: int) -> Any:+ handle = kernel32.CreateFileW(+ path, _FILE_WRITE_DATA | _FILE_READ_ATTRIBUTES, _FILE_SHARE_READ, None, _OPEN_EXISTING, flags, None+ )+ if _invalid(handle):+ raise _error(path)+ return handle++ handle = open_file(_FILE_FLAG_OPEN_REPARSE_POINT)+ try:+ tag = _reparse_tag(handle, path)+ if tag is not None:+ kernel32.CloseHandle(handle)+ handle = None+ if tag & _REPARSE_TAG_NAME_SURROGATE:+ return None+ handle = open_file(0)+ return msvcrt.open_osfhandle(handle, os.O_WRONLY | os.O_BINARY)+ except BaseException:+ if handle is not None:+ kernel32.CloseHandle(handle)+ raise+++def _reparse_tag(handle: Any, path: str) -> Optional[int]:+ """The reparse tag of the open file ``handle``, or ``None`` when it is not a reparse point.++ The attributes come first: a volume without reparse points (FAT) does not answer for their tags.+ """+ from ctypes import wintypes++ class AttributeTagInformation(ctypes.Structure):+ _fields_ = [("FileAttributes", wintypes.DWORD), ("ReparseTag", wintypes.DWORD)]++ if not _basic_information(handle, path).FileAttributes & _FILE_ATTRIBUTE_REPARSE_POINT:+ return None+ information = AttributeTagInformation()+ if not _kernel32().GetFileInformationByHandleEx(+ handle, _FILE_ATTRIBUTE_TAG_INFO, ctypes.byref(information), ctypes.sizeof(information)+ ):+ raise _error(path)+ return int(information.ReparseTag)+++def create_private(path: str) -> int:+ """Create ``path``, which must not exist, for writing, with an owner-only DACL: a binary descriptor.++ The counterpart of a 0600 file, so contents written to it are the owner's alone from the first byte.+ Raises ``OSError`` as ``os.open`` would (``FileExistsError``, ``PermissionError``).+ """+ import msvcrt+ from ctypes import wintypes++ class SecurityAttributes(ctypes.Structure):+ _fields_ = [+ ("nLength", wintypes.DWORD),+ ("lpSecurityDescriptor", ctypes.c_void_p),+ ("bInheritHandle", wintypes.BOOL),+ ]++ kernel32 = _kernel32()+ descriptor = ctypes.c_void_p()+ if not kernel32.advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW(+ _PRIVATE_SDDL, _SDDL_REVISION_1, ctypes.byref(descriptor), None+ ):+ raise _error(path)+ try:+ attributes = SecurityAttributes(ctypes.sizeof(SecurityAttributes), descriptor, False)+ handle = kernel32.CreateFileW(+ path,+ _GENERIC_WRITE,+ _FILE_SHARE_READ_WRITE,+ ctypes.byref(attributes),+ _CREATE_NEW,+ _FILE_ATTRIBUTE_NORMAL,+ None,+ )+ if _invalid(handle):+ raise _error(path)+ finally:+ kernel32.LocalFree(descriptor)+ try:+ return msvcrt.open_osfhandle(handle, os.O_WRONLY | os.O_BINARY)+ except BaseException:+ kernel32.CloseHandle(handle)+ raise+++def open_attributes(path: str) -> int:+ """A descriptor of the file ``path`` that can read its attributes and nothing else, so ``os.fstat`` and+ ``change_time_ns`` work on a file whose ACL denies reading its data or its security."""+ import msvcrt++ kernel32 = _kernel32()+ handle = kernel32.CreateFileW(+ path, _FILE_READ_ATTRIBUTES, _FILE_SHARE_READ_WRITE_DELETE, None, _OPEN_EXISTING, 0, None+ )+ if _invalid(handle):+ raise _error(path)+ try:+ return msvcrt.open_osfhandle(handle, os.O_RDONLY | os.O_BINARY)+ except BaseException:+ kernel32.CloseHandle(handle)+ raise+++def _basic_information(handle: Any, path: str) -> Any:+ from ctypes import wintypes++ class BasicInformation(ctypes.Structure):+ _fields_ = [+ ("CreationTime", ctypes.c_longlong),+ ("LastAccessTime", ctypes.c_longlong),+ ("LastWriteTime", ctypes.c_longlong),+ ("ChangeTime", ctypes.c_longlong),+ ("FileAttributes", wintypes.DWORD),+ ]++ information = BasicInformation()+ if not _kernel32().GetFileInformationByHandleEx(+ handle, _FILE_BASIC_INFO, ctypes.byref(information), ctypes.sizeof(information)+ ):+ raise _error(path)+ return information+++def change_time_ns(fd: int) -> int:+ """The change time of the open file ``fd``: any write or metadata change sets it, and nothing sets it back."""+ import msvcrt++ information = _basic_information(msvcrt.get_osfhandle(fd), f"<descriptor {fd}>")+ return (information.ChangeTime - _UNIX_EPOCH_FILETIME) * 100diff --git a/core/agent_core/tools/write.py b/core/agent_core/tools/write.pyindex 2f34c2118..c79a349c0 100644--- a/core/agent_core/tools/write.py+++ b/core/agent_core/tools/write.py@@ -16,6 +16,7 @@ from typing import Any, Mapping, Optionalfrom core.agent_core.tools.args import ToolInputError, error_result, str_arg, text_resultfrom core.agent_core.tools.base import ToolContext, ToolResult, ToolSpec+from core.agent_core.tools import windows_filesfrom core.agent_core.tools.paths import (KIND_REASON,NotRegularFile,@@ -26,6 +27,8 @@ from core.agent_core.tools.paths import (to_thread_joined,)+_WINDOWS = os.name == "nt"+WRITE_DESCRIPTION = ("Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates ""parent directories."@@ -97,11 +100,17 @@ def _prepare_target(absolute: str) -> Optional[str]:if kind != "regular":return KIND_REASON[kind]# A rename checks the directory, not the file, so a read-only file must be refused here.- if not os.access(target, os.W_OK):+ if not may_write(target):return "permission denied"return None+def may_write(path: str) -> bool:+ """Whether this user may write the existing file ``path``: by its mode on POSIX, and on Windows by its+ ACL, which ``os.access`` does not consult there (it sees only the read-only bit)."""+ return windows_files.may_write(path) if _WINDOWS else os.access(path, os.W_OK)++def write_text(path: str, content: str) -> None:"""UTF-8 without newline translation; ``content`` came through the one sanitizer for model text."""write_bytes(path, content.encode("utf-8"))@@ -131,18 +140,31 @@ class FileIdentity:ctime_ns: int@classmethod- def of(cls, path: str, st: os.stat_result) -> "FileIdentity":- return cls(path, st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns, st.st_ctime_ns)---def _require(expected: Optional[FileIdentity], path: str, st: os.stat_result) -> None:- if expected is not None and FileIdentity.of(path, st) != expected:- raise FileChanged()+ def of(cls, path: str, fd: int) -> "FileIdentity":+ """The identity of the open file ``fd``, which ``path`` names.++ Windows' ``stat`` does not report a change time on every Python (3.10 and 3.11 give the creation time+ as ``st_ctime``), so there it is read from the file itself.+ """+ st = os.fstat(fd)+ ctime_ns = windows_files.change_time_ns(fd) if _WINDOWS else st.st_ctime_ns+ return cls(path, st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns, ctime_ns)++ def matches(self, path: str, st: os.stat_result) -> bool:+ """Whether ``path``, as ``stat`` saw it, is still this file; on Windows it is opened (for its attributes+ alone) to read its change time."""+ if not _WINDOWS:+ return FileIdentity(path, st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns, st.st_ctime_ns) == self+ fd = windows_files.open_attributes(path)+ try:+ return FileIdentity.of(path, fd) == self+ finally:+ os.close(fd)-def _check_publishable(path: str, target: str, expected: Optional[FileIdentity]) -> None:- """Right before the rename: ``path`` still names ``target``, which is absent or a writable regular file- (for a read-modify-write, the one that was read)."""+def _check_publishable(path: str, target: str, expected: Optional[FileIdentity]) -> Optional[os.stat_result]:+ """Right before publication: ``path`` still names ``target``, which is absent or a writable regular file+ (for a read-modify-write, the one that was read). Returns its ``stat``, ``None`` when it is absent."""current = os.path.realpath(path)if current != target:raise FileChanged()@@ -151,27 +173,35 @@ def _check_publishable(path: str, target: str, expected: Optional[FileIdentity])except FileNotFoundError:if expected is not None:raise FileChanged() from None- return+ return Noneif not stat.S_ISREG(st.st_mode):raise NotRegularFile("directory" if stat.S_ISDIR(st.st_mode) else "other")- if not os.access(current, os.W_OK):+ if not may_write(current):raise PermissionError(errno.EACCES, os.strerror(errno.EACCES), current)- _require(expected, current, st)+ if expected is not None and not expected.matches(current, st):+ raise FileChanged()+ return stdef write_bytes(path: str, data: bytes, expected: Optional[FileIdentity] = None) -> None:- """Create or replace ``path`` only once ``data`` is completely on disk.+ """Create or replace ``path`` only once ``data`` is completely on disk (C-7 tools.md section 3).++ The temp file sits beside the real target (through symlinks). Replacing a file, it is private while it+ receives the new contents (0600, or an owner-only DACL on Windows); a new file's temp is created with the+ umask default it keeps, so the process umask is never read or changed.- A failed write (a full disk, a quota) leaves the original, or its absence, as it was. The temp- file sits beside the real target (through symlinks). Replacing a file, it is private (0600) while- it receives the new contents and takes the file's mode once they are fsynced; a new file's temp is- created with the umask default it keeps, so the process umask is never read or changed. Then- ``os.replace`` puts it in place.+ - A new file is the temp file, renamed into place: a failed write (a full disk, a quota) leaves its+ absence as it was.+ - On POSIX an existing file is replaced the same way: the temp file takes its mode once the contents are+ fsynced, and ``os.replace`` puts it in place, so a failed write leaves the original as it was.+ - On Windows an existing file is rewritten in place (``_rewrite``), so it stays the same file object; the+ temp file only shows the volume has room. A crash in the middle of the rewrite can leave it partly+ written.An existing file whose directory does not let a temp file be created is refused- (``NotReplaceable``) rather than written in place, which a full disk could leave half-written.+ (``NotReplaceable``) rather than written without the temp file.- Right before the rename, the path must still name the same target, which must be absent or a+ Right before publication, the path must still name the same target, which must be absent or awritable regular file, as ``write`` first classified it; with ``expected``, it must also be the veryfile that was read. So a file swapped for a FIFO, a directory, or a read-only file, or a pathretargeted to another file, is never written behind the result's back. POSIX has no@@ -185,7 +215,11 @@ def write_bytes(path: str, data: bytes, expected: Optional[FileIdentity] = None)# Short and not derived from the target's name, which may already be at NAME_MAX.tmp = os.path.join(os.path.dirname(target), f".avibe-{secrets.token_hex(6)}.tmp")try:- fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600 if mode is not None else 0o666)+ if _WINDOWS and mode is not None:+ fd = windows_files.create_private(tmp)+ else:+ flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0)+ fd = os.open(tmp, flags, 0o600 if mode is not None else 0o666)except PermissionError:if mode is None:raise@@ -195,11 +229,42 @@ def write_bytes(path: str, data: bytes, expected: Optional[FileIdentity] = None)handle.write(data)handle.flush()os.fsync(handle.fileno())- if mode is not None:+ if mode is not None and not _WINDOWS:os.fchmod(handle.fileno(), mode)- _check_publishable(path, target, expected)- os.replace(tmp, target)+ current = _check_publishable(path, target, expected)+ if _WINDOWS and current is not None:+ os.unlink(tmp) # it showed the volume has room+ _rewrite(path, target, expected, data)+ elif _WINDOWS and mode is not None:+ # Gone since it was read: renaming the private temp file into place would publish a file only+ # this user can read.+ raise FileChanged()+ else:+ os.replace(tmp, target)except BaseException:with contextlib.suppress(FileNotFoundError):os.unlink(tmp)raise+++def _rewrite(path: str, target: str, expected: Optional[FileIdentity], data: bytes) -> None:+ """Rewrite the existing Windows file ``target`` through its own file object: from the start, then cut to length.++ The open does not follow a link, and no other program can write, rename, or delete the file while it is+ held. With it held, the publication checks are made again against it, so the write lands only in the file+ that passed them, whatever the path named between the first check and the open.+ """+ fd = windows_files.open_for_rewriting(target)+ if fd is None: # the name is a link now+ raise FileChanged()+ try:+ held = FileIdentity.of(target, fd)+ if (expected is not None and held != expected) or _check_publishable(path, target, held) is None:+ raise FileChanged()+ view = memoryview(data)+ while view:+ view = view[os.write(fd, view) :]+ os.ftruncate(fd, len(data))+ os.fsync(fd)+ finally:+ os.close(fd)diff --git a/docs/plans/agent-core-contracts/tools.md b/docs/plans/agent-core-contracts/tools.mdindex 7876e3c04..56fb5036f 100644--- a/docs/plans/agent-core-contracts/tools.md+++ b/docs/plans/agent-core-contracts/tools.md@@ -67,6 +67,37 @@ creates parent directories.`Result: `Successfully wrote to {path}`. Writes are serialized per canonical path with `edit`.+How a write lands (Avibe); `edit` publishes the same way. The new content is always written to a temp file beside the+file first, the owner's alone while it is there (0600 on POSIX, an owner-only DACL on Windows).++- A new file is that temp file, renamed into place, so a failure leaves its absence as it was.+- On POSIX an existing file is replaced by the temp file, which takes its mode, so a failure (a full disk, a quota)+ leaves it as it was.+- On Windows an existing file is rewritten in place, so it stays the same file object: its owner, DACL, audit+ entries, integrity label, encryption, compression, named streams (the Mark of the Web among them), hard links, and+ creation time stay as they were. The temp file only shows the volume has room, and is removed. The file is opened+ without following a link, and no other program can write, rename, or delete it while it is open; one that already+ has it open that way makes the write fail before a byte changes. With the file held, the checks made before+ publication (a writable regular file, which the path still names; for `edit`, the file it read) are made again+ against it, so the write lands only in the file that passed them.+- A file whose ACL denies writing it is refused, as a read-only one is.+- `edit` writes only over the file it read: the same file id, size, mtime, and change time (Windows' NTFS change+ time, which `stat` does not report there on every Python).++Known limits, kept as they are:++- A POSIX replacement is a new file of this user's, as with an editor that saves by renaming. Replacing a+ group-writable file another account owns makes this user its owner. The mode is carried over, but not ACLs,+ extended attributes (file capabilities among them), or hard links. POSIX has no compare-and-rename, so a change+ between the last check and the rename is not seen; a rename replaces a name and never writes through a link.+- A Windows write to an existing file is an ordinary in-place write, as an editor's save is. It is not all or nothing:+ a crash, a quota, or a failure of the disk itself in the middle of the rewrite can leave the file partly written.+ Its growth is charged to the file owner's quota, and the temp file, which shows the volume has room, reserves+ nothing.+- A new file is published by renaming its temp file, on both platforms, as before. A directory that lets this user+ create files but neither rename nor delete them (a Windows drop folder) refuses the rename and the removal alike,+ so the temp file stays there, with the ACL the new file would have had.+## 4. `edit`Description: `Edit a single file using exact text replacement. Every edits[].oldText must match a unique,diff --git a/modules/agents/vibey/agent.py b/modules/agents/vibey/agent.pyindex feae086e7..3ef1c8ca3 100644--- a/modules/agents/vibey/agent.py+++ b/modules/agents/vibey/agent.py@@ -121,8 +121,11 @@ def _relative_to(cwd: str) -> Callable[[str], str]:absolute = os.path.abspath(os.path.join(cwd, os.path.expanduser(path)) if cwd else os.path.expanduser(path))if not cwd:return absolute- shown = os.path.relpath(absolute, cwd)- return absolute if shown.startswith("../..") else shown+ try:+ shown = os.path.relpath(absolute, cwd)+ except ValueError: # Windows: another drive has no relative path+ return absolute+ return absolute if shown.startswith(os.path.join(os.pardir, os.pardir)) else shownreturn relativediff --git a/tests/agent_core/tools/test_file_kinds.py b/tests/agent_core/tools/test_file_kinds.pyindex eb8214f0a..ae8e9cc51 100644--- a/tests/agent_core/tools/test_file_kinds.py+++ b/tests/agent_core/tools/test_file_kinds.py@@ -41,6 +41,24 @@ NOT_REGULAR = {"fifo", "socket", "device", "link_device"}DIRECTORY = {"directory", "link_directory"}ABSENT = {"dangling", "missing"}NOT_ROOT = pytest.mark.skipif(hasattr(os, "geteuid") and os.geteuid() == 0, reason="root ignores file modes")+WINDOWS = os.name == "nt"+# Kinds Windows cannot make: a mode there is only the read-only bit, it has no FIFOs, the socket here needs+# /tmp, and a link to NUL does not resolve.+POSIX_ONLY = {"unreadable", "fifo", "socket", "link_device", "in_locked_dir"}+NOT_ON_WINDOWS = pytest.mark.skipif(WINDOWS, reason="a POSIX kind of file")+# The OS's own words for the rows where Windows says it differently.+if WINDOWS:+ NO_PATH = "the system cannot find the path specified"+ ABSENT_REASON, UNDER_FILE_REASON = NO_PATH, NO_PATH+ LOOP_REASON = "the name of the file cannot be resolved by the system"+ # Write creates a missing parent; a file in the way of it says so.+ UNDER_FILE_WRITE_REASON = "cannot create a file when that file already exists"+ UNDER_FILE_CODE, LOOP_CODE = "ENOENT", "EINVAL"+else:+ ABSENT_REASON, UNDER_FILE_REASON = "no such file or directory", "not a directory"+ LOOP_REASON = "too many levels of symbolic links"+ UNDER_FILE_WRITE_REASON = UNDER_FILE_REASON+ UNDER_FILE_CODE, LOOP_CODE = "ENOTDIR", "ELOOP"@pytest.fixture@@ -50,7 +68,7 @@ def place(tmp_path):def make(kind: str) -> str:if kind in ("regular", "read_only", "unreadable"):- (tmp_path / "f").write_text("hello\n")+ (tmp_path / "f").write_text("hello\n", encoding="utf-8", newline="")mode = {"regular": 0o644, "read_only": 0o444, "unreadable": 0o000}[kind]os.chmod(tmp_path / "f", mode)cleanups.append(lambda: os.chmod(tmp_path / "f", 0o644))@@ -72,7 +90,7 @@ def place(tmp_path):if kind == "device":return os.devnullif kind == "link_regular":- (tmp_path / "f").write_text("hello\n")+ (tmp_path / "f").write_text("hello\n", encoding="utf-8", newline="")(tmp_path / "l").symlink_to(tmp_path / "f")return "l"if kind == "link_directory":@@ -88,12 +106,12 @@ def place(tmp_path):if kind == "missing":return "missing/sub/f"if kind == "under_file":- (tmp_path / "f").write_text("hello\n")+ (tmp_path / "f").write_text("hello\n", encoding="utf-8", newline="")return "f/x"if kind == "in_locked_dir":# A writable file in a directory this user cannot add to.(tmp_path / "ro").mkdir()- (tmp_path / "ro" / "f").write_text("hello\n")+ (tmp_path / "ro" / "f").write_text("hello\n", encoding="utf-8", newline="")os.chmod(tmp_path / "ro", 0o555)cleanups.append(lambda: os.chmod(tmp_path / "ro", 0o755))return "ro/f"@@ -127,7 +145,15 @@ async def _run(tool, arguments, ctx, tmp_path):def _cases(kinds):- return [pytest.param(kind, marks=NOT_ROOT) if kind in ("unreadable", "in_locked_dir") else kind for kind in kinds]+ def marks(kind):+ found = []+ if kind in ("unreadable", "in_locked_dir"):+ found.append(NOT_ROOT)+ if kind in POSIX_ONLY:+ found.append(NOT_ON_WINDOWS)+ return found++ return [pytest.param(kind, marks=marks(kind)) for kind in kinds]@pytest.mark.parametrize("kind", _cases(KINDS))@@ -141,11 +167,11 @@ async def test_read_has_a_defined_result_for_every_kind(tmp_path, make_ctx, placreturnreason = {"unreadable": "permission denied",- "under_file": "not a directory",- "loop": "too many levels of symbolic links",+ "under_file": UNDER_FILE_REASON,+ "loop": LOOP_REASON,**dict.fromkeys(DIRECTORY, "it is a directory"),**dict.fromkeys(NOT_REGULAR, "it is not a regular file"),- **dict.fromkeys(ABSENT, "no such file or directory"),+ **dict.fromkeys(ABSENT, ABSENT_REASON),}[kind]assert (result.is_error, result_text(result)) == (True, f"Cannot read {path}: {reason}.")@@ -164,7 +190,7 @@ async def test_write_has_a_defined_result_for_every_kind(tmp_path, make_ctx, plaif kind in ("regular", "link_regular", "dangling", "missing"):assert (result.is_error, result_text(result)) == (False, f"Successfully wrote to {path}")target = {"regular": "f", "link_regular": "f", "dangling": "missing/f", "missing": "missing/sub/f"}[kind]- assert (tmp_path / target).read_text() == "new\n"+ assert (tmp_path / target).read_text(encoding="utf-8") == "new\n"if kind in ("link_regular", "dangling"):assert (tmp_path / path).is_symlink()return@@ -173,8 +199,8 @@ async def test_write_has_a_defined_result_for_every_kind(tmp_path, make_ctx, pla"unreadable": "permission denied",# Replacing needs a temp file beside it; writing in place could leave it half-written (ENOSPC)."in_locked_dir": "its directory is not writable, so the file cannot be replaced safely",- "under_file": "not a directory",- "loop": "too many levels of symbolic links",+ "under_file": UNDER_FILE_WRITE_REASON,+ "loop": LOOP_REASON,**dict.fromkeys(DIRECTORY, "it is a directory"),**dict.fromkeys(NOT_REGULAR, "it is not a regular file"),}[kind]@@ -183,9 +209,9 @@ async def test_write_has_a_defined_result_for_every_kind(tmp_path, make_ctx, plaif before is not None:assert os.lstat(path if os.path.isabs(path) else tmp_path / path).st_mode == beforeif kind == "read_only":- assert (tmp_path / "f").read_text() == "hello\n"+ assert (tmp_path / "f").read_text(encoding="utf-8") == "hello\n"if kind == "in_locked_dir":- assert (tmp_path / "ro" / "f").read_text() == "hello\n"+ assert (tmp_path / "ro" / "f").read_text(encoding="utf-8") == "hello\n"@pytest.mark.parametrize("kind", _cases(KINDS))@@ -197,15 +223,15 @@ async def test_edit_has_a_defined_result_for_every_kind(tmp_path, make_ctx, placif kind in ("regular", "link_regular"):assert (result.is_error, result_text(result)) == (False, f"Successfully replaced 1 block(s) in {path} (line 1).")- assert (tmp_path / "f").read_text() == "bye\n"+ assert (tmp_path / "f").read_text(encoding="utf-8") == "bye\n"assert (tmp_path / path).is_symlink() == (kind == "link_regular")returndetail = {"read_only": "Error code: EACCES.","unreadable": "Error code: EACCES.","in_locked_dir": "Its directory is not writable, so the file cannot be replaced safely.",- "loop": "Error code: ELOOP.",- "under_file": "Error code: ENOTDIR.",+ "loop": f"Error code: {LOOP_CODE}.",+ "under_file": f"Error code: {UNDER_FILE_CODE}.",**dict.fromkeys(DIRECTORY, "Error code: EISDIR."),**dict.fromkeys(NOT_REGULAR, "It is not a regular file."),**dict.fromkeys(ABSENT, "Error code: ENOENT."),@@ -214,4 +240,4 @@ async def test_edit_has_a_defined_result_for_every_kind(tmp_path, make_ctx, placif kind == "read_only":assert stat.S_IMODE(os.stat(tmp_path / "f").st_mode) == 0o444if kind == "in_locked_dir":- assert (tmp_path / "ro" / "f").read_text() == "hello\n"+ assert (tmp_path / "ro" / "f").read_text(encoding="utf-8") == "hello\n"diff --git a/tests/agent_core/tools/test_read.py b/tests/agent_core/tools/test_read.pyindex cb966a9c2..8871468fc 100644--- a/tests/agent_core/tools/test_read.py+++ b/tests/agent_core/tools/test_read.py@@ -25,7 +25,7 @@ async def _read(make_ctx, **arguments):async def test_head_is_cut_at_the_line_limit_with_the_next_offset(tmp_path, make_ctx):- (tmp_path / "log.txt").write_text("".join(f"line {i}\n" for i in range(1, 2501)))+ (tmp_path / "log.txt").write_text("".join(f"line {i}\n" for i in range(1, 2501)), encoding="utf-8", newline="")result = await _read(make_ctx, path="log.txt")@@ -37,7 +37,7 @@ async def test_head_is_cut_at_the_line_limit_with_the_next_offset(tmp_path, makeasync def test_head_is_cut_at_the_byte_limit(tmp_path, make_ctx):# 1,000-byte lines: the first takes 1,000 bytes and each further one 1,001, so 51 fit in 51,200.- (tmp_path / "wide.txt").write_text("".join(f"{i:04d}" + "x" * 996 + "\n" for i in range(100)))+ (tmp_path / "wide.txt").write_text("".join(f"{i:04d}" + "x" * 996 + "\n" for i in range(100)), encoding="utf-8", newline="")text = result_text(await _read(make_ctx, path="wide.txt"))@@ -46,7 +46,7 @@ async def test_head_is_cut_at_the_byte_limit(tmp_path, make_ctx):async def test_offset_and_limit_name_the_remaining_lines(tmp_path, make_ctx):- (tmp_path / "ten.txt").write_text("\n".join(f"l{i}" for i in range(1, 11)))+ (tmp_path / "ten.txt").write_text("\n".join(f"l{i}" for i in range(1, 11)), encoding="utf-8", newline="")text = result_text(await _read(make_ctx, path="ten.txt", offset=3, limit=4))@@ -54,7 +54,7 @@ async def test_offset_and_limit_name_the_remaining_lines(tmp_path, make_ctx):async def test_offset_beyond_the_end_is_an_error(tmp_path, make_ctx):- (tmp_path / "two.txt").write_text("a\nb")+ (tmp_path / "two.txt").write_text("a\nb", encoding="utf-8", newline="")result = await _read(make_ctx, path="two.txt", offset=5)@@ -64,7 +64,7 @@ async def test_offset_beyond_the_end_is_an_error(tmp_path, make_ctx):@pytest.mark.parametrize(("arguments", "message"), [({"offset": 0}, "offset"), ({"limit": 2.5}, "limit")])async def test_offset_and_limit_are_integers_of_at_least_one(tmp_path, make_ctx, arguments, message):- (tmp_path / "ten.txt").write_text("a\nb\n")+ (tmp_path / "ten.txt").write_text("a\nb\n", encoding="utf-8", newline="")result = await _read(make_ctx, path="ten.txt", **arguments)@@ -260,6 +260,7 @@ async def test_read_takes_the_image_size_from_the_file_it_reads(tmp_path, make_cassert "[Image omitted: the file is" in result_text(result)[email protected](not hasattr(os, "mkfifo"), reason="Windows has no FIFOs")async def test_a_fifo_swapped_in_after_the_check_does_not_block_read(tmp_path, make_ctx, monkeypatch):os.mkfifo(tmp_path / "p")monkeypatch.setattr(read_module, "target_kind", lambda path: "regular") # it was a file when checkeddiff --git a/tests/agent_core/tools/test_write_edit.py b/tests/agent_core/tools/test_write_edit.pyindex 43c8d2b07..0e9c4b729 100644--- a/tests/agent_core/tools/test_write_edit.py+++ b/tests/agent_core/tools/test_write_edit.py@@ -4,13 +4,19 @@ from __future__ import annotationsimport asyncioimport errno+import functools+import getpassimport osimport randomimport re+import secrets+import shutilimport stat+import subprocessimport threadingimport timeimport tracemalloc+from pathlib import Pathimport pytest@@ -25,6 +31,10 @@ from core.agent_core.tools.read import ReadToolfrom core.agent_core.tools.write import WriteToolfrom tests.agent_core.tools.conftest import result_text+WINDOWS = os.name == "nt"+# The OS's own words, which Windows chooses differently.+NO_SUCH_FILE = "the system cannot find the file specified" if WINDOWS else "no such file or directory"+async def test_write_creates_parent_directories_and_keeps_bytes(tmp_path, make_ctx):result = await WriteTool().execute({"path": "a/b/notes.txt", "content": "第一行\r\nsecond\n"}, make_ctx())@@ -35,7 +45,7 @@ async def test_write_creates_parent_directories_and_keeps_bytes(tmp_path, make_casync def test_a_failed_edit_write_leaves_the_original_intact(tmp_path, make_ctx, monkeypatch):- (tmp_path / "f.txt").write_text("original\n")+ (tmp_path / "f.txt").write_text("original\n", encoding="utf-8", newline="")def disk_full(fd):raise OSError(errno.ENOSPC, "No space left on device")@@ -44,14 +54,14 @@ async def test_a_failed_edit_write_leaves_the_original_intact(tmp_path, make_ctxresult = await _edit(make_ctx, "f.txt", {"oldText": "original", "newText": "changed"})assert (result.is_error, result_text(result)) == (True, "Could not edit file: f.txt. Error code: ENOSPC.")- assert (tmp_path / "f.txt").read_text() == "original\n"+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == "original\n"assert sorted(p.name for p in tmp_path.iterdir()) == ["f.txt"]@pytest.mark.parametrize("existing", [True, False])async def test_a_failed_write_leaves_the_original_intact(tmp_path, make_ctx, monkeypatch, existing):if existing:- (tmp_path / "f.txt").write_text("original\n")+ (tmp_path / "f.txt").write_text("original\n", encoding="utf-8", newline="")def disk_full(fd):raise OSError(errno.ENOSPC, "No space left on device")@@ -61,22 +71,22 @@ async def test_a_failed_write_leaves_the_original_intact(tmp_path, make_ctx, monassert result.is_errorif existing:- assert (tmp_path / "f.txt").read_text() == "original\n"+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == "original\n"assert sorted(p.name for p in tmp_path.iterdir()) == (["f.txt"] if existing else [])async def test_write_goes_through_a_symlink_and_keeps_the_mode(tmp_path, make_ctx):target = tmp_path / "real.sh"- target.write_text("old\n")- target.chmod(0o751)+ target.write_text("old\n", encoding="utf-8", newline="")+ target.chmod(0o751) # on Windows, a mode is only the read-only bit(tmp_path / "link.sh").symlink_to(target)result = await WriteTool().execute({"path": "link.sh", "content": "new\n"}, make_ctx())assert not result.is_error, result_text(result)assert (tmp_path / "link.sh").is_symlink()- assert target.read_text() == "new\n"- assert stat.S_IMODE(target.stat().st_mode) == 0o751+ assert target.read_text(encoding="utf-8") == "new\n"+ assert stat.S_IMODE(target.stat().st_mode) == (0o666 if WINDOWS else 0o751)async def _edit(make_ctx, path, *edits, **extra):@@ -84,30 +94,30 @@ async def _edit(make_ctx, path, *edits, **extra):async def test_disjoint_edits_apply_against_the_original(tmp_path, make_ctx):- (tmp_path / "f.py").write_text("a = 1\nb = 2\nc = 3\n")+ (tmp_path / "f.py").write_text("a = 1\nb = 2\nc = 3\n", encoding="utf-8", newline="")result = await _edit(make_ctx, "f.py", {"oldText": "a = 1", "newText": "a = 10"}, {"oldText": "c = 3", "newText": "c = 30"})assert result_text(result) == "Successfully replaced 2 block(s) in f.py (lines 1, 3)."- assert (tmp_path / "f.py").read_text() == "a = 10\nb = 2\nc = 30\n"+ assert (tmp_path / "f.py").read_text(encoding="utf-8") == "a = 10\nb = 2\nc = 30\n"assert "+1 a = 10" in result.details["diff"]async def test_normalized_match_rewrites_only_the_touched_lines(tmp_path, make_ctx):original = "title = “Hello” \nkeep = ‘as is’ \nlast = 1\n"- (tmp_path / "f.txt").write_text(original)+ (tmp_path / "f.txt").write_text(original, encoding="utf-8", newline="")# ASCII quotes and no trailing spaces: only Pi's normalized tier finds it.result = await _edit(make_ctx, "f.txt", {"oldText": 'title = "Hello"', "newText": 'title = "Hi"'})assert not result.is_error, result_text(result)- assert (tmp_path / "f.txt").read_text() == 'title = "Hi"\nkeep = ‘as is’ \nlast = 1\n'+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == 'title = "Hi"\nkeep = ‘as is’ \nlast = 1\n'async def test_replace_all_replaces_every_occurrence(tmp_path, make_ctx):- (tmp_path / "f.txt").write_text("foo(1)\nbar\nfoo(2)\nfoo(3)\n")+ (tmp_path / "f.txt").write_text("foo(1)\nbar\nfoo(2)\nfoo(3)\n", encoding="utf-8", newline="")result = await _edit(make_ctx,@@ -118,7 +128,7 @@ async def test_replace_all_replaces_every_occurrence(tmp_path, make_ctx):# The model learns how many occurrences each item replaced without reading the file again.assert result_text(result) == "Successfully replaced 4 occurrences in f.txt (lines 1-4; edits[0]: 3, edits[1]: 1)."- assert (tmp_path / "f.txt").read_text() == "baz(1)\nqux\nbaz(2)\nbaz(3)\n"+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == "baz(1)\nqux\nbaz(2)\nbaz(3)\n"@pytest.mark.parametrize(("content", "summary"), [@@ -126,7 +136,7 @@ async def test_replace_all_replaces_every_occurrence(tmp_path, make_ctx):("color=red\n", "Successfully replaced 1 occurrence in f.txt (line 1)."),])async def test_a_single_replace_all_reports_its_occurrences(tmp_path, make_ctx, content, summary):- (tmp_path / "f.txt").write_text(content)+ (tmp_path / "f.txt").write_text(content, encoding="utf-8", newline="")result = await _edit(make_ctx, "f.txt", {"oldText": "red", "newText": "blue", "replaceAll": True})@@ -254,7 +264,7 @@ async def test_text_copied_from_read_edits_the_first_line_of_a_bom_file(tmp_pathasync def test_edit_takes_the_size_from_the_file_it_reads(tmp_path, make_ctx, monkeypatch):"""The file grew after a check by path: the limit holds on the descriptor actually read."""- (tmp_path / "f.txt").write_text("hello\n" + "x" * 2000)+ (tmp_path / "f.txt").write_text("hello\n" + "x" * 2000, encoding="utf-8", newline="")monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 1000)monkeypatch.setattr(edit_module.os.path, "getsize", lambda path: 10) # what an earlier check by path saw@@ -262,12 +272,13 @@ async def test_edit_takes_the_size_from_the_file_it_reads(tmp_path, make_ctx, moassert result.is_errorassert result_text(result).startswith("File f.txt is 2.0KB, over the 1000B edit limit.")- assert (tmp_path / "f.txt").read_text().startswith("hello")+ assert (tmp_path / "f.txt").read_text(encoding="utf-8").startswith("hello")[email protected](WINDOWS, reason="Windows keeps a file private through its ACL, not its mode")async def test_replacement_contents_stay_private_until_the_rename(tmp_path, make_ctx, monkeypatch):secret = tmp_path / "token"- secret.write_text("old\n")+ secret.write_text("old\n", encoding="utf-8", newline="")secret.chmod(0o600)seen = []real_fsync = write_module.os.fsync@@ -282,7 +293,7 @@ async def test_replacement_contents_stay_private_until_the_rename(tmp_path, makeassert not result.is_error, result_text(result)assert seen and all(mode & 0o077 == 0 for mode in seen)assert stat.S_IMODE(secret.stat().st_mode) == 0o600- assert secret.read_text() == "new secret\n"+ assert secret.read_text(encoding="utf-8") == "new secret\n"_WRITE_FIRST = {"path": "f.txt", "content": "first\n"}@@ -302,7 +313,7 @@ async def test_a_cancelled_write_lands_before_the_next_writer_takes_the_lock(tmp_path, make_ctx, monkeypatch, first, cancels, worker_fails):"""Cancelling an await does not stop its worker thread; the lock is held until the worker is done."""- (tmp_path / "f.txt").write_text("original\n")+ (tmp_path / "f.txt").write_text("original\n", encoding="utf-8", newline="")entered, release, renamed = threading.Event(), threading.Event(), threading.Event()real_fsync, real_replace, calls, renames = write_module.os.fsync, write_module.os.replace, [], []expected_renames = 1 if worker_fails else 2@@ -316,14 +327,19 @@ async def test_a_cancelled_write_lands_before_the_next_writer_takes_the_lock(raise OSError(errno.ENOSPC, "No space left on device")real_fsync(fd)- def counted_replace(source, target):- real_replace(source, target)- renames.append(target)- if len(renames) == expected_renames:- renamed.set()+ def counted(publish):+ def publish_and_count(*args):+ publish(*args)+ renames.append(args)+ if len(renames) == expected_renames:+ renamed.set()++ return publish_and_countmonkeypatch.setattr(write_module.os, "fsync", first_fsync_blocks)- monkeypatch.setattr(write_module.os, "replace", counted_replace)+ # An existing file is replaced by a rename, or on Windows rewritten in place.+ monkeypatch.setattr(write_module.os, "replace", counted(real_replace))+ monkeypatch.setattr(write_module, "_rewrite", counted(write_module._rewrite))tool = WriteTool() if "content" in first else EditTool()cancelled = asyncio.ensure_future(tool.execute(first, make_ctx()))await asyncio.to_thread(entered.wait, 10)@@ -339,7 +355,7 @@ async def test_a_cancelled_write_lands_before_the_next_writer_takes_the_lock(# The cancel propagates even when the worker failed; it is never turned into a result.assert isinstance(outcomes[0], asyncio.CancelledError)assert not outcomes[1].is_error, result_text(outcomes[1])- assert (tmp_path / "f.txt").read_text() == "second\n"+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == "second\n"@pytest.mark.parametrize("change", ["replaced", "retargeted", "rewritten", "rewritten-same-size-same-mtime"])@@ -347,26 +363,34 @@ async def test_an_edit_publishes_only_over_the_file_it_read(tmp_path, make_ctx,"""Another writer (bash, an editor) changed the file after edit read it: nothing is written over it."""(tmp_path / "d").mkdir()real, other, link = tmp_path / "d" / "a.txt", tmp_path / "d" / "b.txt", tmp_path / "d" / "f.txt"- real.write_text("alpha\n")- other.write_text("other\n")+ real.write_text("alpha\n", encoding="utf-8", newline="")+ other.write_text("other\n", encoding="utf-8", newline="")link.symlink_to(real)real_plan = edit_module._plan_editsdef plan_then_change(*args):planned = real_plan(*args)if change == "replaced":- (tmp_path / "d" / "new").write_text("unrelated text\n")+ (tmp_path / "d" / "new").write_text("unrelated text\n", encoding="utf-8", newline="")os.replace(tmp_path / "d" / "new", real)elif change == "retargeted":link.unlink()link.symlink_to(other)elif change == "rewritten":- real.write_text("unrelated text\n")+ real.write_text("unrelated text\n", encoding="utf-8", newline="")else:- # Same size, in place, mtime put back (as rsync -t or tar do): only ctime still shows it.+ # Same size, in place, mtime put back (as rsync -t or tar do): only ctime still shows it, once the+ # filesystem's clock has moved past the read (Windows stamps a file at its timer's resolution).before = os.stat(real)- real.write_text("ALPHA\n")- os.utime(real, ns=(before.st_atime_ns, before.st_mtime_ns))+ read_ctime = planned[4].ctime_ns+ deadline = time.monotonic() + 5+ while True:+ real.write_text("ALPHA\n", encoding="utf-8", newline="")+ os.utime(real, ns=(before.st_atime_ns, before.st_mtime_ns))+ with open(real, "rb") as handle:+ if os.fstat(handle.fileno()).st_ctime_ns != read_ctime or time.monotonic() > deadline:+ break+ time.sleep(0.005)return plannedmonkeypatch.setattr(edit_module, "_plan_edits", plan_then_change)@@ -380,14 +404,21 @@ async def test_an_edit_publishes_only_over_the_file_it_read(tmp_path, make_ctx,"retargeted": ("alpha\n", "other\n"),"rewritten-same-size-same-mtime": ("ALPHA\n", "other\n"),}.get(change, ("unrelated text\n", "other\n"))- assert (real.read_text(), other.read_text()) == expected+ assert (real.read_text(encoding="utf-8"), other.read_text(encoding="utf-8")) == expected[email protected]("swap", ["fifo", "read-only", "directory"])+ "swap",+ [+ pytest.param("fifo", marks=pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="Windows has no FIFOs")),+ "read-only",+ "directory",+ ],+)async def test_write_revalidates_the_target_right_before_replacing_it(tmp_path, make_ctx, monkeypatch, swap):"""Another process swapped the file after write classified it: nothing that is not a writable regular fileis ever replaced."""- (tmp_path / "f").write_text("old\n")+ (tmp_path / "f").write_text("old\n", encoding="utf-8", newline="")real_prepare = write_module._prepare_targetdef prepare_then_swap(absolute):@@ -411,15 +442,357 @@ async def test_write_revalidates_the_target_right_before_replacing_it(tmp_path,kind = os.lstat(tmp_path / "f").st_modeassert {"fifo": stat.S_ISFIFO, "read-only": stat.S_ISREG, "directory": stat.S_ISDIR}[swap](kind)if swap == "read-only":- assert (tmp_path / "f").read_text() == "theirs\n"+ assert (tmp_path / "f").read_text(encoding="utf-8") == "theirs\n"assert [p.name for p in tmp_path.iterdir()] == ["f"]+def _icacls(path, *arguments):+ """``icacls`` itself, so the ACL is checked by Windows' own tool rather than by the code under test."""+ return subprocess.run(["icacls", str(path), *arguments], capture_output=True, text=True, check=True).stdout+++async def _replace_with(tool, make_ctx, path):+ if tool == "write":+ return await WriteTool().execute({"path": path, "content": "new\n"}, make_ctx())+ return await _edit(make_ctx, path, {"oldText": "old", "newText": "new"})++[email protected](not WINDOWS, reason="a Windows ACL")[email protected]("tool", ["write", "edit"])+async def test_a_written_file_keeps_an_acl_of_its_own(tmp_path, make_ctx, tool):+ """The replacement starts with what its directory passes on; a file kept private by its own ACL stays so."""+ target = tmp_path / "secret.txt"+ target.write_bytes(b"old\n")+ _icacls(target, "/inheritance:r", "/grant:r", f"{getpass.getuser()}:(F)")+ before = _icacls(target)++ result = await _replace_with(tool, make_ctx, "secret.txt")++ assert not result.is_error, result_text(result)+ assert target.read_bytes() == b"new\n"+ assert _icacls(target) == before++[email protected](not WINDOWS, reason="a Windows ACL")[email protected]("tool", ["write", "edit"])+async def test_new_contents_stay_the_owners_until_they_are_written(tmp_path, make_ctx, monkeypatch, tool):+ """The temp file is created owner-only, as a 0600 file is on POSIX: not with the directory's wider ACL."""+ # Python's mkdtemp, which made tmp_path, gives it entries without automatic inheritance, so its files list+ # theirs as their own; a user's directory inherits automatically, as it does after /reset.+ _icacls(tmp_path, "/reset")+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ before = _icacls(target)+ seen = []+ real_fsync = write_module.os.fsync++ def watch(fd):+ # The temp copy's fsync; the rewrite of the file itself syncs too, once the copy is gone.+ for temp in (path for path in tmp_path.iterdir() if path.name.startswith(".avibe-")):+ seen.append(_icacls(temp).replace(str(temp), "").split("\n\n")[0].split())+ real_fsync(fd)++ monkeypatch.setattr(write_module.os, "fsync", watch)+ result = await _replace_with(tool, make_ctx, "f.txt")++ assert not result.is_error, result_text(result)+ assert seen == [["OWNER", "RIGHTS:(F)", "NT", "AUTHORITY\\SYSTEM:(F)"]]+ assert _icacls(target) == before+++def _owner(path):+ """The file's owner, by name, read with the Win32 API directly rather than through the code under test."""+ import ctypes+ from ctypes import wintypes++ advapi32 = ctypes.WinDLL("advapi32", use_last_error=True)+ advapi32.GetNamedSecurityInfoW.argtypes = [wintypes.LPCWSTR, ctypes.c_int, wintypes.DWORD] + [+ ctypes.POINTER(ctypes.c_void_p) if index in (0, 4) else ctypes.c_void_p for index in range(5)+ ]+ advapi32.LookupAccountSidW.argtypes = [+ wintypes.LPCWSTR, ctypes.c_void_p, wintypes.LPWSTR, ctypes.POINTER(wintypes.DWORD), wintypes.LPWSTR,+ ctypes.POINTER(wintypes.DWORD), ctypes.POINTER(wintypes.DWORD),+ ]+ owner, descriptor = ctypes.c_void_p(), ctypes.c_void_p()+ assert advapi32.GetNamedSecurityInfoW(str(path), 1, 1, ctypes.byref(owner), None, None, None, ctypes.byref(descriptor)) == 0+ name, domain = ctypes.create_unicode_buffer(256), ctypes.create_unicode_buffer(256)+ name_size, domain_size, use = wintypes.DWORD(256), wintypes.DWORD(256), wintypes.DWORD()+ assert advapi32.LookupAccountSidW(None, owner, name, name_size, domain, domain_size, use)+ ctypes.windll.kernel32.LocalFree(descriptor)+ return f"{domain.value}\\{name.value}"++[email protected](not WINDOWS, reason="Windows rewrites in place")[email protected]("owner", ["this user", "another account"])[email protected]("tool", ["write", "edit"])+async def test_on_windows_an_existing_file_is_rewritten_in_place_whoever_owns_it(tmp_path, make_ctx, tool, owner):+ """It stays the same file object: id, owner, ACL, streams, and its hard links all see the new content."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ zone = "[ZoneTransfer]\r\nZoneId=3\r\n"+ with open(f"{target}:Zone.Identifier", "w", encoding="ascii", newline="") as stream:+ stream.write(zone)+ os.link(target, tmp_path / "link.txt")+ if owner == "another account":+ _icacls(target, "/setowner", "NT AUTHORITY\\SYSTEM")+ before = (_owner(target), _icacls(target), os.stat(target).st_ino)++ result = await _replace_with(tool, make_ctx, "f.txt")++ assert not result.is_error, result_text(result)+ assert target.read_bytes() == (tmp_path / "link.txt").read_bytes() == b"new\n"+ assert (_owner(target), _icacls(target), os.stat(target).st_ino) == before+ with open(f"{target}:Zone.Identifier", encoding="ascii", newline="") as stream:+ assert stream.read() == zone+ assert sorted(path.name for path in tmp_path.iterdir()) == ["f.txt", "link.txt"]++[email protected](not WINDOWS, reason="a Windows ACL")+async def test_a_file_this_user_may_write_but_not_inspect_is_written(tmp_path, make_ctx):+ """Writing the data is allowed and reading the file's security is not: neither the replacement's ACL copy nor+ ``os.open`` (which asks for it) may stand in the way."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ _icacls(target, "/setowner", "NT AUTHORITY\\SYSTEM")+ # Nothing undoes this: removing the file needs only the directory's permission.+ _icacls(target, "/deny", f"{getpass.getuser()}:(RC)")++ result = await WriteTool().execute({"path": "f.txt", "content": "new\n"}, make_ctx())++ assert not result.is_error, result_text(result)+ assert _read_data_only(target) == b"new\n" # open() asks for the denied right too+ assert [path.name for path in tmp_path.iterdir()] == ["f.txt"]++[email protected](not WINDOWS, reason="a Windows ACL")+async def test_a_file_whose_acl_denies_reading_its_attributes_is_written(tmp_path, make_ctx):+ """NTFS lets whoever may list a directory read the attributes of the files in it, whatever their own ACLs+ say, so checking the file the rewrite opened asks for nothing that writing it does not already have."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ _icacls(target, "/deny", f"{getpass.getuser()}:(RA)")+ try:+ assert "(DENY)(RA)" in _icacls(target)+ result = await WriteTool().execute({"path": "f.txt", "content": "new\n"}, make_ctx())+ finally:+ _icacls(target, "/remove:d", getpass.getuser())++ assert not result.is_error, result_text(result)+ assert target.read_bytes() == b"new\n"+ assert [path.name for path in tmp_path.iterdir()] == ["f.txt"]+++def _read_data_only(path):+ import ctypes+ import msvcrt+ from ctypes import wintypes++ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)+ kernel32.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, ctypes.c_void_p] + [+ wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE+ ]+ kernel32.CreateFileW.restype = wintypes.HANDLE+ handle = kernel32.CreateFileW(str(path), 0x0001, 0x7, None, 3, 0, None) # FILE_READ_DATA, OPEN_EXISTING+ assert handle and handle != ctypes.c_void_p(-1).value, ctypes.get_last_error()+ with os.fdopen(msvcrt.open_osfhandle(handle, os.O_RDONLY | os.O_BINARY), "rb") as handle:+ return handle.read()++[email protected](not WINDOWS, reason="a Windows integrity label")+async def test_a_written_file_keeps_its_integrity_label(tmp_path, make_ctx):+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ _icacls(target, "/setintegritylevel", "low")+ before = _icacls(target)+ assert "Low Mandatory Level" in before++ result = await WriteTool().execute({"path": "f.txt", "content": "new\n"}, make_ctx())++ assert not result.is_error, result_text(result)+ assert target.read_bytes() == b"new\n"+ assert _icacls(target) == before++[email protected](not WINDOWS, reason="Windows file times")+def test_the_change_time_an_edit_compares_moves_with_every_write(tmp_path):+ """Python before 3.12 reports a Windows file's creation time as ``st_ctime``, so an edit reads the change time+ from the file itself: a same-size rewrite whose mtime is put back still moves it."""+ from core.agent_core.tools import windows_files++ path = tmp_path / "f.txt"+ path.write_bytes(b"one\n")+ original = os.stat(path)++ def change_time():+ with open(path, "rb") as handle:+ return windows_files.change_time_ns(handle.fileno())++ before = change_time()+ deadline = time.monotonic() + 5+ while True: # until the filesystem's clock has moved past the first write (its timer's resolution)+ path.write_bytes(b"two\n")+ os.utime(path, ns=(original.st_atime_ns, original.st_mtime_ns))+ if change_time() != before or time.monotonic() > deadline:+ break+ time.sleep(0.005)++ assert change_time() > before+ assert os.stat(path).st_mtime_ns == original.st_mtime_ns+ assert os.stat(path).st_birthtime_ns == original.st_birthtime_ns++[email protected](not WINDOWS, reason="Windows file metadata")[email protected]("tool", ["write", "edit"])+async def test_a_written_file_keeps_its_named_streams_and_encryption(tmp_path, make_ctx, tool):+ """A downloaded file keeps its Mark of the Web, and a file encrypted on its own stays encrypted."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ zone = "[ZoneTransfer]\r\nZoneId=3\r\n"+ with open(f"{target}:Zone.Identifier", "w", encoding="ascii", newline="") as stream:+ stream.write(zone)+ subprocess.run(["cipher", "/e", str(target)], capture_output=True, check=True)+ assert os.stat(target).st_file_attributes & stat.FILE_ATTRIBUTE_ENCRYPTED++ result = await _replace_with(tool, make_ctx, "f.txt")++ assert not result.is_error, result_text(result)+ assert target.read_bytes() == b"new\n"+ with open(f"{target}:Zone.Identifier", encoding="ascii", newline="") as stream:+ assert stream.read() == zone+ assert os.stat(target).st_file_attributes & stat.FILE_ATTRIBUTE_ENCRYPTED+ assert [p.name for p in tmp_path.iterdir()] == ["f.txt"]++[email protected](not WINDOWS, reason="Windows file sharing")+async def test_a_reader_holding_the_file_does_not_stop_a_write(tmp_path, make_ctx):+ """A rename onto a file another program has open fails on Windows; a rewrite in place shares it with readers."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ with open(target, "rb"):+ result = await WriteTool().execute({"path": "f.txt", "content": "new\n"}, make_ctx())++ assert not result.is_error, result_text(result)+ assert target.read_bytes() == b"new\n"+ assert [path.name for path in tmp_path.iterdir()] == ["f.txt"]++[email protected](not os.environ.get("AVIBE_TEST_FAT_DIR"), reason="needs a FAT volume (AVIBE_TEST_FAT_DIR)")[email protected]("tool", ["write", "edit"])+async def test_a_file_on_a_volume_without_acls_is_written(make_ctx, tool):+ """FAT keeps no owners or ACLs: the owner-only temp file and the rewrite in place work there too."""+ directory = Path(os.environ["AVIBE_TEST_FAT_DIR"]) / f"replace-{tool}-{secrets.token_hex(4)}"+ directory.mkdir()+ try:+ (directory / "f.txt").write_bytes(b"old\n")+ result = await _replace_with(tool, functools.partial(make_ctx, cwd=str(directory)), "f.txt")++ assert not result.is_error, result_text(result)+ assert (directory / "f.txt").read_bytes() == b"new\n"+ assert [p.name for p in directory.iterdir()] == ["f.txt"]+ finally:+ shutil.rmtree(directory, ignore_errors=True)++[email protected](not WINDOWS, reason="Windows file sharing")+async def test_an_in_place_rewrite_lets_no_other_writer_in(tmp_path, make_ctx):+ """Another program has the file open for writing: the rewrite fails before a byte changes, and leaves nothing."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")++ with open(target, "r+b"):+ result = await WriteTool().execute({"path": "f.txt", "content": "new\n"}, make_ctx())++ assert result.is_error+ assert result_text(result).startswith("Cannot write f.txt: ")+ assert target.read_bytes() == b"old\n"+ assert [path.name for path in tmp_path.iterdir()] == ["f.txt"]++[email protected](not WINDOWS, reason="Windows rewrites in place")[email protected]("swap", ["the file for a symbolic link", "its directory for a junction"])[email protected]("tool", ["write", "edit"])+async def test_a_file_swapped_after_the_last_check_is_not_written_through(tmp_path, make_ctx, monkeypatch, tool, swap):+ """Another process swaps the file, or a directory on its path, for a link to another file between the last+ check by path and the open: the rewrite checks what it opened, so the other file is never written."""+ import _winapi++ project, elsewhere = tmp_path / "project", tmp_path / "elsewhere"+ project.mkdir()+ elsewhere.mkdir()+ (project / "f.txt").write_bytes(b"old\n")+ (elsewhere / "f.txt").write_bytes(b"old\n")+ real_open = write_module.windows_files.open_for_rewriting++ def swap_then_open(target):+ if swap == "the file for a symbolic link":+ os.replace(project / "f.txt", tmp_path / "moved.txt")+ os.symlink(elsewhere / "f.txt", project / "f.txt")+ else:+ os.rename(project, tmp_path / "moved")+ _winapi.CreateJunction(str(elsewhere), str(project))+ return real_open(target)++ monkeypatch.setattr(write_module.windows_files, "open_for_rewriting", swap_then_open)+ result = await _replace_with(tool, functools.partial(make_ctx, cwd=str(project)), "f.txt")++ expected = {+ "write": "Cannot write f.txt: it changed while it was being written; write it again.",+ "edit": "Could not edit file: f.txt. It changed while the edit was being applied; read it again.",+ }[tool]+ assert (result.is_error, result_text(result)) == (True, expected)+ assert (elsewhere / "f.txt").read_bytes() == b"old\n"+ moved = tmp_path / ("moved.txt" if swap == "the file for a symbolic link" else "moved/f.txt")+ assert moved.read_bytes() == b"old\n"++[email protected](not WINDOWS, reason="a Windows reparse point")+async def test_a_file_with_a_reparse_point_of_its_own_is_written(tmp_path, make_ctx, monkeypatch):+ """A reparse point that is not a link (a OneDrive placeholder's, here as its tag alone, since a runner has no+ sync provider) belongs to the file itself: the file is opened again through the filter behind it, and written."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ asked = []++ def as_a_cloud_file(handle, path):+ asked.append(path)+ return 0x9000601A # IO_REPARSE_TAG_CLOUD_6++ monkeypatch.setattr(write_module.windows_files, "_reparse_tag", as_a_cloud_file)+ result = await WriteTool().execute({"path": "f.txt", "content": "new\n"}, make_ctx())++ assert not result.is_error, result_text(result)+ assert len(asked) == 1 # of the open that did not follow it; the file's own open is not asked again+ assert target.read_bytes() == b"new\n"+ assert [p.name for p in tmp_path.iterdir()] == ["f.txt"]++[email protected](not WINDOWS, reason="a Windows ACL")[email protected]("tool", ["write", "edit"])+async def test_a_file_whose_acl_denies_writing_is_not_replaced(tmp_path, make_ctx, tool):+ """The directory still lets the file be renamed over, which would get past an ACL that denies writing it."""+ target = tmp_path / "f.txt"+ target.write_bytes(b"old\n")+ # Only writing the data is denied: reading it, and so planning an edit, still works.+ _icacls(target, "/deny", f"{getpass.getuser()}:(WD)")+ try:+ result = await _replace_with(tool, make_ctx, "f.txt")+ finally:+ _icacls(target, "/remove:d", getpass.getuser())++ expected = {+ "write": "Cannot write f.txt: permission denied.",+ "edit": "Could not edit file: f.txt. Error code: EACCES.",+ }[tool]+ assert (result.is_error, result_text(result)) == (True, expected)+ assert target.read_bytes() == b"old\n"+ assert [p.name for p in tmp_path.iterdir()] == ["f.txt"]++@pytest.mark.parametrize("tool", ["write", "edit"])async def test_a_name_at_the_length_limit_can_be_replaced(tmp_path, make_ctx, tool):"""The temp file beside the target must not be longer than the target's own (valid) name."""name = "n" * 251 + ".txt" # 255 bytes, the usual NAME_MAX- (tmp_path / name).write_text("old\n")+ (tmp_path / name).write_text("old\n", encoding="utf-8", newline="")if tool == "write":result = await WriteTool().execute({"path": name, "content": "new\n"}, make_ctx())@@ -427,14 +800,14 @@ async def test_a_name_at_the_length_limit_can_be_replaced(tmp_path, make_ctx, toresult = await _edit(make_ctx, name, {"oldText": "old", "newText": "new"})assert not result.is_error, result_text(result)- assert (tmp_path / name).read_text() == "new\n"+ assert (tmp_path / name).read_text(encoding="utf-8") == "new\n"@pytest.mark.parametrize("tool", ["write", "edit"])async def test_a_path_retargeted_during_the_write_is_not_written(tmp_path, make_ctx, monkeypatch, tool):"""The path names another file by the time of the rename: the old target is not written behind its back."""- (tmp_path / "a.txt").write_text("alpha\n")- (tmp_path / "b.txt").write_text("beta\n")+ (tmp_path / "a.txt").write_text("alpha\n", encoding="utf-8", newline="")+ (tmp_path / "b.txt").write_text("beta\n", encoding="utf-8", newline="")(tmp_path / "f.txt").symlink_to(tmp_path / "a.txt")real_fsync = write_module.os.fsync@@ -452,7 +825,7 @@ async def test_a_path_retargeted_during_the_write_is_not_written(tmp_path, make_expected = "Could not edit file: f.txt. It changed while the edit was being applied; read it again."assert (result.is_error, result_text(result)) == (True, expected)- assert ((tmp_path / "a.txt").read_text(), (tmp_path / "b.txt").read_text()) == ("alpha\n", "beta\n")+ assert ((tmp_path / "a.txt").read_text(encoding="utf-8"), (tmp_path / "b.txt").read_text(encoding="utf-8")) == ("alpha\n", "beta\n")async def test_new_files_keep_the_umask_default(tmp_path, make_ctx):@@ -466,7 +839,7 @@ async def test_new_files_keep_the_umask_default(tmp_path, make_ctx):async def test_write_and_edit_sanitize_model_text_the_same_way(tmp_path, make_ctx):await WriteTool().execute({"path": "w.txt", "content": "a\ud800b"}, make_ctx())- (tmp_path / "e.txt").write_text("a-b")+ (tmp_path / "e.txt").write_text("a-b", encoding="utf-8", newline="")await _edit(make_ctx, "e.txt", {"oldText": "-", "newText": "\udfff"})assert (tmp_path / "w.txt").read_bytes() == "a\ufffdb".encode()@@ -475,7 +848,7 @@ async def test_write_and_edit_sanitize_model_text_the_same_way(tmp_path, make_ctasync def test_the_display_diff_is_bounded_by_lines_not_only_bytes(tmp_path, make_ctx):"""Repetitive lines made difflib quadratic (Codex measured over 22 s); the changed middle is all it sees."""- (tmp_path / "rep.txt").write_text("x\n" * 5000 + "target\n" + "x\n" * 5000)+ (tmp_path / "rep.txt").write_text("x\n" * 5000 + "target\n" + "x\n" * 5000, encoding="utf-8", newline="")started = time.process_time()result = await _edit(make_ctx, "rep.txt", {"oldText": "target", "newText": "changed"})@@ -555,7 +928,7 @@ async def test_the_display_diff_and_patch_are_pis(tmp_path, make_ctx, original,)async def test_edit_work_stays_linear_on_adversarial_files(tmp_path, make_ctx, content, edit):"""CPU time, which a quadratic match would take seconds of; disk waits do not count."""- (tmp_path / "f.txt").write_text(content)+ (tmp_path / "f.txt").write_text(content, encoding="utf-8", newline="")started = time.process_time()await _edit(make_ctx, "f.txt", edit)@@ -581,7 +954,7 @@ async def test_edit_work_stays_linear_on_adversarial_files(tmp_path, make_ctx, c)async def test_edit_memory_is_bounded_by_its_budgets_not_by_the_file(tmp_path, make_ctx, content, edit, is_error):"""Python objects per line or per match cost hundreds of bytes each, in the process every Session shares."""- (tmp_path / "f.txt").write_text(content)+ (tmp_path / "f.txt").write_text(content, encoding="utf-8", newline="")tracemalloc.start()try:result = await _edit(make_ctx, "f.txt", edit)@@ -600,7 +973,7 @@ async def test_edit_memory_is_bounded_by_its_budgets_not_by_the_file(tmp_path, m)async def test_nfkc_expansion_has_a_budget_and_says_so(tmp_path, make_ctx, content):"""NFKC can make one character eighteen (U+FDFA): loose matching stops at a budget and says why."""- (tmp_path / "f.txt").write_text(content + "\u2019marker\u2019\n")+ (tmp_path / "f.txt").write_text(content + "\u2019marker\u2019\n", encoding="utf-8", newline="")tracemalloc.start()started = time.process_time()try:@@ -621,30 +994,30 @@ async def test_nfkc_expansion_has_a_budget_and_says_so(tmp_path, make_ctx, conteasync def test_a_large_file_with_a_few_compatibility_characters_still_matches_loosely(tmp_path, make_ctx):"""An ellipsis (NFKC "...") in a 2 MiB file costs a few characters of growth, not the fuzzy tier."""body = "".join(f"line {i} \u2026 text\n" for i in range(110_000)) # about 2 MiB, not in NFKC- (tmp_path / "f.txt").write_text(body + "say \u2018marker\u2019\n")+ (tmp_path / "f.txt").write_text(body + "say \u2018marker\u2019\n", encoding="utf-8", newline="")result = await _edit(make_ctx, "f.txt", {"oldText": "say 'marker'", "newText": "said"})assert not result.is_error, result_text(result)- assert (tmp_path / "f.txt").read_text().endswith("\u2026 text\nsaid\n")+ assert (tmp_path / "f.txt").read_text(encoding="utf-8").endswith("\u2026 text\nsaid\n")async def test_a_long_minified_line_still_matches_loosely(tmp_path, make_ctx):"""A 200 KiB line is normalized in pieces split before ASCII characters, which NFKC never composes with."""line = "x=1;" * 50_000 + "\u2026 say \u2018marker\u2019"- (tmp_path / "f.min.js").write_text(line + "\n")+ (tmp_path / "f.min.js").write_text(line + "\n", encoding="utf-8", newline="")result = await _edit(make_ctx, "f.min.js", {"oldText": "say 'marker'", "newText": "said"})assert not result.is_error, result_text(result)# A normalized match rewrites its whole line from the normalized view (ledger 13), so "…" became "...".- assert (tmp_path / "f.min.js").read_text() == "x=1;" * 50_000 + "... said\n"+ assert (tmp_path / "f.min.js").read_text(encoding="utf-8") == "x=1;" * 50_000 + "... said\n"async def test_edit_refuses_files_with_more_lines_than_it_plans(tmp_path, make_ctx, monkeypatch):monkeypatch.setattr(edit_module, "MAX_EDIT_LINES", 100)- (tmp_path / "ok.txt").write_text("x\n" * 99 + "t")- (tmp_path / "big.txt").write_text("x\r\n" * 100 + "t")+ (tmp_path / "ok.txt").write_text("x\n" * 99 + "t", encoding="utf-8", newline="")+ (tmp_path / "big.txt").write_text("x\r\n" * 100 + "t", encoding="utf-8", newline="")ok = await _edit(make_ctx, "ok.txt", {"oldText": "t", "newText": "u"})big = await _edit(make_ctx, "big.txt", {"oldText": "t", "newText": "u"})@@ -686,18 +1059,18 @@ async def test_edit_refuses_files_with_more_lines_than_it_plans(tmp_path, make_casync def test_replace_all_has_a_budget(tmp_path, make_ctx, monkeypatch, edits, message):monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 1000)monkeypatch.setattr(edit_diff_module, "MAX_REPLACEMENTS", 10 if len(edits) == 1 else 19)- (tmp_path / "f.txt").write_text("a," * 11 + "zz\n" + "c," * 10 + "\n")+ (tmp_path / "f.txt").write_text("a," * 11 + "zz\n" + "c," * 10 + "\n", encoding="utf-8", newline="")result = await _edit(make_ctx, "f.txt", *edits)assert (result.is_error, result_text(result)) == (True, message)- assert (tmp_path / "f.txt").read_text() == "a," * 11 + "zz\n" + "c," * 10 + "\n"+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == "a," * 11 + "zz\n" + "c," * 10 + "\n"async def test_edit_work_has_a_budget_over_all_edits(tmp_path, make_ctx, monkeypatch):"""Each edit scans the whole file, holding the GIL every Session shares: edits × size is bounded."""monkeypatch.setattr(edit_diff_module, "MAX_EDIT_SCAN_CHARS", 1000)- (tmp_path / "f.txt").write_text("".join(f"line {i}\n" for i in range(100))) # 790 characters+ (tmp_path / "f.txt").write_text("".join(f"line {i}\n" for i in range(100)), encoding="utf-8", newline="") # 790 charactersedits = [{"oldText": f"line {i}\n", "newText": f"LINE {i}\n"} for i in range(2)]one = await _edit(make_ctx, "f.txt", edits[0])@@ -713,7 +1086,7 @@ async def test_edit_work_has_a_budget_over_all_edits(tmp_path, make_ctx, monkeypasync def test_the_display_diff_never_splits_a_result_with_too_many_lines(tmp_path, make_ctx):"""A result under the diff byte gate can still have a million lines; their count is checked first."""- (tmp_path / "f.txt").write_text("t\n" + "x\n" * 100)+ (tmp_path / "f.txt").write_text("t\n" + "x\n" * 100, encoding="utf-8", newline="")tracemalloc.start()try:result = await _edit(make_ctx, "f.txt", {"oldText": "t", "newText": "ab\n" * 300_000})@@ -726,7 +1099,7 @@ async def test_the_display_diff_never_splits_a_result_with_too_many_lines(tmp_paasync def test_a_large_changed_middle_skips_the_display_diff(tmp_path, make_ctx):- (tmp_path / "f.txt").write_text("".join(f"line {i}\n" for i in range(3000)))+ (tmp_path / "f.txt").write_text("".join(f"line {i}\n" for i in range(3000)), encoding="utf-8", newline="")result = await _edit(make_ctx,@@ -740,7 +1113,7 @@ async def test_a_large_changed_middle_skips_the_display_diff(tmp_path, make_ctx)async def test_a_planned_result_over_the_limit_is_refused_and_its_diff_skipped(tmp_path, make_ctx, monkeypatch):- (tmp_path / "small.txt").write_text("seed\n")+ (tmp_path / "small.txt").write_text("seed\n", encoding="utf-8", newline="")monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 1000)monkeypatch.setattr(edit_module, "MAX_DIFF_BYTES", 400)@@ -753,11 +1126,11 @@ async def test_a_planned_result_over_the_limit_is_refused_and_its_diff_skipped(t"File small.txt would be over the 1000B edit limit after this edit. ""Use bash (for example sed or a short script) to change files this large.")- assert (tmp_path / "small.txt").read_text() == "x" * 600 + "\n"+ assert (tmp_path / "small.txt").read_text(encoding="utf-8") == "x" * 600 + "\n"async def test_files_over_the_edit_limit_are_refused_and_large_diffs_skipped(tmp_path, make_ctx, monkeypatch):- (tmp_path / "big.log").write_text("line\n" * 100)+ (tmp_path / "big.log").write_text("line\n" * 100, encoding="utf-8", newline="")monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 400)refused = await _edit(make_ctx, "big.log", {"oldText": "line", "newText": "x", "replaceAll": True})@@ -767,7 +1140,7 @@ async def test_files_over_the_edit_limit_are_refused_and_large_diffs_skipped(tmp"File big.log is 500B, over the 400B edit limit. ""Use bash (for example sed or a short script) to change files this large.")- assert (tmp_path / "big.log").read_text() == "line\n" * 100+ assert (tmp_path / "big.log").read_text(encoding="utf-8") == "line\n" * 100monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 10_000)monkeypatch.setattr(edit_module, "MAX_DIFF_BYTES", 400)@@ -788,12 +1161,12 @@ async def test_bom_and_line_endings_are_preserved(tmp_path, make_ctx, ending):async def test_an_exact_match_is_unique_even_with_a_normalized_twin(tmp_path, make_ctx):- (tmp_path / "f.txt").write_text('say "Hi"\nsay \u201cHi\u201d\n')+ (tmp_path / "f.txt").write_text('say "Hi"\nsay \u201cHi\u201d\n', encoding="utf-8", newline="")result = await _edit(make_ctx, "f.txt", {"oldText": 'say "Hi"', "newText": 'say "Hello"'})assert not result.is_error, result_text(result)- assert (tmp_path / "f.txt").read_text() == 'say "Hello"\nsay \u201cHi\u201d\n'+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == 'say "Hello"\nsay \u201cHi\u201d\n'L, R = "\u201c", "\u201d" # smart double quotes@@ -838,12 +1211,12 @@ L, R = "\u201c", "\u201d" # smart double quotes],)async def test_each_edit_matches_in_its_own_tier(tmp_path, make_ctx, original, edits, expected):- (tmp_path / "f.txt").write_text(original)+ (tmp_path / "f.txt").write_text(original, encoding="utf-8", newline="")result = await _edit(make_ctx, "f.txt", *edits)assert not result.is_error, result_text(result)- assert (tmp_path / "f.txt").read_text() == expected+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == expected_BREAKS = ["\r\n", "\r", "\n"]@@ -939,19 +1312,19 @@ async def test_an_edit_never_changes_bytes_outside_the_lines_it_replaces(tmp_pat)async def test_a_failing_edit_writes_nothing(tmp_path, make_ctx, edits, message):original = "alpha beta\ndup one\ndup two\n"- (tmp_path / "f.txt").write_text(original)+ (tmp_path / "f.txt").write_text(original, encoding="utf-8", newline="")result = await _edit(make_ctx, "f.txt", *edits)assert result.is_errorassert result_text(result) == message- assert (tmp_path / "f.txt").read_text() == original+ assert (tmp_path / "f.txt").read_text(encoding="utf-8") == original@pytest.mark.parametrize(("tool", "arguments", "expected"),[- (ReadTool(), {"path": "a\ud800"}, "Cannot read a\ufffd: no such file or directory."),+ (ReadTool(), {"path": "a\ud800"}, f"Cannot read a\ufffd: {NO_SUCH_FILE}."),(WriteTool(), {"path": "a\udfff", "content": "x"}, "Successfully wrote to a\ufffd"),],)@@ -966,7 +1339,8 @@ async def test_lone_surrogates_in_a_path_are_sanitized(tmp_path, make_ctx, tool,[(ReadTool(), {"path": "a\x00b"}),(WriteTool(), {"path": "a\x00b", "content": "x"}),- (EditTool(), {"path": "file:///tmp/a%00b", "edits": [{"oldText": "a", "newText": "b"}]}),+ # With a drive, the URL is absolute on Windows too, so the NUL byte is what refuses it.+ (EditTool(), {"path": "file:///C:/tmp/a%00b", "edits": [{"oldText": "a", "newText": "b"}]}),],)async def test_a_path_no_file_can_have_is_an_error_result(make_ctx, tool, arguments):diff --git a/tests/test_ci_pipeline.py b/tests/test_ci_pipeline.pyindex 0225084ca..2928e14b2 100644--- a/tests/test_ci_pipeline.py+++ b/tests/test_ci_pipeline.py@@ -74,7 +74,7 @@ def test_ci_uv_installation_is_exact_and_cache_ownership_is_preserved():}assert set(owners) == {"unit-test-shards", "migration-release-guard", "install-upgrade-shards",- "show-router-integration", "memory-insight-contract",+ "show-router-integration", "memory-insight-contract", "windows-agent",}cache_keys = {}for name, job in owners.items():@@ -102,6 +102,11 @@ def test_ci_uv_installation_is_exact_and_cache_ownership_is_preserved():cache_keys[name] = keyfirst_consumer = next(step for step in steps if step.get("run", "").startswith("uv "))assert steps.index(install) < steps.index(cache) < steps.index(first_consumer)+ if "windows" in f"{job['runs-on']} {job.get('strategy', {})}":+ # uv's Windows default is %LOCALAPPDATA%\uv\cache, not the path the cache step keeps.+ redirect, = [step for step in steps if "UV_CACHE_DIR" in step.get("run", "")]+ assert "UV_CACHE_DIR=$HOME\\.cache\\uv" in redirect["run"]+ assert steps.index(redirect) < steps.index(first_consumer)assert len({cache_keys[name] for name in owners}) == 1diff --git a/tests/test_vibey_agent.py b/tests/test_vibey_agent.pyindex 5c83f9e3d..f6bd5b4ad 100644--- a/tests/test_vibey_agent.py+++ b/tests/test_vibey_agent.py@@ -1632,9 +1632,14 @@ async def test_a_relative_tool_path_is_shown_under_the_runs_cwd(engine, session,project = tmp_path / "project"shown = _relative_to(str(project))# The tool reads src/app.py under the run's cwd; the line shows it as such, not under the controller's cwd.- assert shown("src/app.py") == "src/app.py"- assert shown(str(project / "core" / "foo.py")) == "core/foo.py"- assert shown("/etc/hosts") == "/etc/hosts"+ assert shown("src/app.py") == os.path.join("src", "app.py")+ assert shown(str(project / "core" / "foo.py")) == os.path.join("core", "foo.py")+ outside = os.path.abspath(os.path.join(os.sep, "etc", "hosts"))+ assert shown(outside) == outside+ if os.name == "nt":+ # Another drive has no relative path at all.+ other_drive = "Z:\\x.py" if not str(project).upper().startswith("Z:") else "Y:\\x.py"+ assert shown(other_drive) == other_driveasync def test_the_system_prompt_lists_every_tool_the_run_offers(engine, session, tmp_path, published) -> None: