From 042e315368b5e9b30b985132353daa450bfc234d Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:47:21 +0800 Subject: [PATCH 1/9] fix(jev): capture scoped evidence safely on native Windows Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> --- .../loopx-jev/src/loopx_jev/drift_capture.py | 141 +++++++- .../loopx-jev/tests/test_drift_capture.py | 307 ++++++++++++++++++ packages/loopx-jev/tests/test_protocol.py | 21 +- 3 files changed, 452 insertions(+), 17 deletions(-) create mode 100644 packages/loopx-jev/tests/test_drift_capture.py diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index 5199ea224d..b19193ff38 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -5,10 +5,12 @@ import difflib import hashlib import os -from pathlib import Path +import stat import subprocess import tempfile -from typing import Any +from contextlib import ExitStack +from pathlib import Path +from typing import Any, BinaryIO from .protocol import request_bytes @@ -43,11 +45,16 @@ def validate_paths(repo: Path, paths: list[str]) -> list[str]: raise ValueError("scope_requires_one_to_32_unique_files") for name in paths: path = Path(name) + parts = ( + tuple(part.casefold() for part in path.parts) + if os.name == "nt" + else path.parts + ) if ( not name or path.is_absolute() or ".." in path.parts - or ".git" in path.parts + or ".git" in parts or name != path.as_posix() or any(ord(char) < 32 for char in name) ): @@ -58,8 +65,107 @@ def validate_paths(repo: Path, paths: list[str]) -> list[str]: return sorted(paths) +def _windows_identity( + info: os.stat_result, + *, + directory: bool = False, +) -> tuple[int, int, int, int]: + attributes = getattr(info, "st_file_attributes", None) + if attributes is None: + raise ValueError("unsupported_capture_platform") + if stat.S_ISLNK(info.st_mode) or attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: + raise ValueError("scope_escapes_workspace") + if not (stat.S_ISDIR if directory else stat.S_ISREG)(info.st_mode): + raise ValueError("scope_requires_regular_files") + if not info.st_dev or not info.st_ino: + raise ValueError("capture_identity_unavailable") + return info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns + + +def _windows_descriptor(target: Path, *, directory: bool = False) -> int: + import ctypes + import msvcrt + from ctypes import wintypes + + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + create = kernel.CreateFileW + create.argtypes = ( + wintypes.LPCWSTR, + wintypes.DWORD, + wintypes.DWORD, + wintypes.LPVOID, + wintypes.DWORD, + wintypes.DWORD, + wintypes.HANDLE, + ) + create.restype = wintypes.HANDLE + kernel.CloseHandle.argtypes = (wintypes.HANDLE,) + kernel.CloseHandle.restype = wintypes.BOOL + filename = os.path.abspath(target) + if not filename.startswith("\\\\?\\"): + filename = ( + "\\\\?\\UNC\\" + filename[2:] + if filename.startswith("\\\\") + else "\\\\?\\" + filename + ) + # Directories deny write/delete sharing to pin traversal; OPEN_REPARSE_POINT + # opens a raced-in link itself. BACKUP_SEMANTICS permits directory handles. + flags = 0x00200000 | (0x02000000 if directory else 0) + handle = create( + filename, 0x80000000, 0x1 if directory else 0x7, None, 3, flags, None + ) + if handle == wintypes.HANDLE(-1).value: + raise ctypes.WinError(ctypes.get_last_error()) + try: + return msvcrt.open_osfhandle(handle, os.O_RDONLY | os.O_BINARY | os.O_NOINHERIT) + except BaseException: + kernel.CloseHandle(handle) + raise + + +def _windows_open(target: Path) -> BinaryIO: + descriptor = _windows_descriptor(target) + try: + return os.fdopen(descriptor, "rb") + except BaseException: + os.close(descriptor) + raise + + +def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: + with ExitStack() as parents: + current = repo + for part in ("", *target.relative_to(repo).parts[:-1]): + current = current / part + try: + expected = _windows_identity(os.lstat(current), directory=True)[:2] + except FileNotFoundError: + return None + descriptor = _windows_descriptor(current, directory=True) + parents.callback(os.close, descriptor) + if _windows_identity(os.fstat(descriptor), directory=True)[:2] != expected: + raise ValueError("workspace_changed_during_capture") + try: + observed = os.lstat(target) + except FileNotFoundError: + return None + expected_file = _windows_identity(observed) + with _windows_open(target) as stream: + if _windows_identity(os.fstat(stream.fileno())) != expected_file: + raise ValueError("workspace_changed_during_capture") + raw = stream.read(MAX_BYTES + 1) + opened = os.fstat(stream.fileno()) + if ( + _windows_identity(opened) != expected_file + or _windows_identity(os.lstat(target)) != expected_file + ): + raise ValueError("workspace_changed_during_capture") + return raw, opened.st_mode & 0o111 + + def capture(repo: Path, paths: list[str]) -> dict[str, Any]: - if not hasattr(os, "O_NOFOLLOW"): + windows = os.name == "nt" and not hasattr(os, "O_NOFOLLOW") + if not windows and not hasattr(os, "O_NOFOLLOW"): raise ValueError("unsupported_capture_platform") repo = repo.resolve() paths = validate_paths(repo, paths) @@ -73,15 +179,24 @@ def capture(repo: Path, paths: list[str]) -> dict[str, Any]: target = repo / name if target.is_symlink() or not target.resolve().is_relative_to(repo): raise ValueError("scope_escapes_workspace") - if not target.exists(): - files[name] = None - continue - if not target.is_file(): - raise ValueError("scope_requires_regular_files") - # O_NOFOLLOW also rejects a final-component symlink introduced after check. - with os.fdopen(os.open(target, os.O_RDONLY | os.O_NOFOLLOW), "rb") as stream: - raw = stream.read(MAX_BYTES + 1) - mode = os.fstat(stream.fileno()).st_mode & 0o111 + if windows: + captured = _windows_capture(repo, target) + if captured is None: + files[name] = None + continue + raw, mode = captured + else: + if not target.exists(): + files[name] = None + continue + if not target.is_file(): + raise ValueError("scope_requires_regular_files") + # O_NOFOLLOW also rejects a final-component symlink introduced after check. + with os.fdopen( + os.open(target, os.O_RDONLY | os.O_NOFOLLOW), "rb" + ) as stream: + raw = stream.read(MAX_BYTES + 1) + mode = os.fstat(stream.fileno()).st_mode & 0o111 size += len(raw) if size > MAX_BYTES or b"\0" in raw: raise ValueError("oversized_or_binary_scope") diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py new file mode 100644 index 0000000000..961e44b22b --- /dev/null +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -0,0 +1,307 @@ +"""File identity and no-follow boundaries of scoped workspace capture.""" + +import os +import stat +import subprocess +from types import SimpleNamespace + +import pytest +from loopx_jev import drift_capture + + +@pytest.fixture +def repo(tmp_path): + for args in ( + ("init", "-q"), + ( + "-c", + "user.name=Fixture", + "-c", + "user.email=fixture@example.invalid", + "commit", + "--allow-empty", + "-qm", + "baseline", + ), + ): + subprocess.run( + ["git", "-C", str(tmp_path), *args], check=True, capture_output=True + ) + return tmp_path + + +def stat_fields(info, **changes): + return SimpleNamespace( + **{ + **{ + key: getattr(info, key) + for key in ( + "st_mode", + "st_dev", + "st_ino", + "st_size", + "st_mtime_ns", + ) + }, + "st_file_attributes": getattr(info, "st_file_attributes", 0), + **changes, + } + ) + + +@pytest.fixture +def windows(monkeypatch): + platform = SimpleNamespace(**vars(os)) + platform.name = "nt" + if hasattr(platform, "O_NOFOLLOW"): + del platform.O_NOFOLLOW + platform.lstat = lambda path: stat_fields(os.lstat(path)) + platform.fstat = lambda fd: stat_fields(os.fstat(fd)) + monkeypatch.setattr(drift_capture, "os", platform) + if os.name != "nt": + # Native Windows exercises CreateFileW; other hosts simulate its IO boundary. + monkeypatch.setattr( + drift_capture, + "_windows_descriptor", + lambda path, directory=False: os.open(path, os.O_RDONLY), + ) + return platform + + +def test_windows_regular_file_without_nofollow(repo, windows): + (repo / "file.txt").write_bytes(b"before\r\nafter\n") + captured = drift_capture.stable_capture(repo, ["file.txt", "missing.txt"]) + assert captured["files"]["file.txt"]["text"] == "before\r\nafter\n" + assert captured["files"]["missing.txt"] is None + + +@pytest.mark.parametrize("name", [".GIT/config", ".gIt/config"]) +def test_windows_rejects_case_insensitive_git_metadata(repo, windows, name): + with pytest.raises(ValueError, match="invalid_scope_path"): + drift_capture.capture(repo, [name]) + + +@pytest.mark.parametrize("directory", [False, True]) +def test_windows_rejects_symlink(repo, windows, directory): + target = repo / "original" + target.mkdir() if directory else target.write_text("original") + link = repo / "link" + try: + link.symlink_to(target, target_is_directory=directory) + except OSError: + # Symlink creation can require a Windows privilege; still test lstat policy. + link.mkdir() if directory else link.write_text("original") + original = windows.lstat + windows.lstat = lambda path: ( + stat_fields(original(path), st_mode=stat.S_IFLNK) + if path == link + else original(path) + ) + name = "link/missing.txt" if directory else "link" + with pytest.raises(ValueError, match="scope_escapes_workspace"): + drift_capture.capture(repo, [name]) + + +@pytest.mark.parametrize("component", ["file.txt", "nested"]) +def test_windows_rejects_reparse_point(repo, windows, component): + (repo / "nested").mkdir() + target = repo / "nested/file.txt" + target.write_text("original") + original = windows.lstat + flagged = target if component == "file.txt" else repo / component + windows.lstat = lambda path: ( + stat_fields( + original(path), st_file_attributes=stat.FILE_ATTRIBUTE_REPARSE_POINT + ) + if path == flagged + else original(path) + ) + with pytest.raises(ValueError, match="scope_escapes_workspace"): + drift_capture.capture(repo, ["nested/file.txt"]) + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows junction") +def test_windows_rejects_real_junction(repo, windows): + target = repo / "directory" + target.mkdir() + (target / "file.txt").write_text("original") + link = repo / "junction" + # Junctions do not require the symbolic-link privilege. + subprocess.run( + ["cmd", "/d", "/c", "mklink", "/J", str(link), str(target)], + check=True, + capture_output=True, + ) + try: + assert link.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT + with pytest.raises(ValueError, match="scope_escapes_workspace"): + drift_capture.capture(repo, ["junction/file.txt"]) + finally: + link.rmdir() + + +@pytest.mark.parametrize("field", ["st_dev", "st_ino", "st_size", "st_mtime_ns"]) +def test_windows_rejects_opened_identity_mismatch(repo, windows, monkeypatch, field): + (repo / "file.txt").write_text("original") + original = windows.fstat + windows.fstat = lambda fd: stat_fields( + original(fd), **{field: getattr(original(fd), field) + 1} + ) + opened = drift_capture._windows_open + + def unread(path): + stream = opened(path) + monkeypatch.setattr( + stream, "read", lambda *args: pytest.fail("read unverified bytes") + ) + return stream + + monkeypatch.setattr(drift_capture, "_windows_open", unread) + with pytest.raises(ValueError, match="workspace_changed_during_capture"): + drift_capture.capture(repo, ["file.txt"]) + + +@pytest.mark.parametrize("field", ["st_dev", "st_ino"]) +@pytest.mark.parametrize("boundary", ["lstat", "fstat"]) +def test_windows_fails_closed_without_identity(repo, windows, field, boundary): + (repo / "file.txt").write_text("original") + original = getattr(windows, boundary) + setattr(windows, boundary, lambda value: stat_fields(original(value), **{field: 0})) + with pytest.raises(ValueError, match="capture_identity_unavailable"): + drift_capture.capture(repo, ["file.txt"]) + + +def test_windows_rejects_mutation_during_read(repo, windows, monkeypatch): + target = repo / "file.txt" + target.write_text("original") + opened = drift_capture._windows_open + + def replacing(path): + stream = opened(path) + original = stream.read + + def read(*args): + raw = original(*args) + target.write_text("changed during read") + return raw + + monkeypatch.setattr(stream, "read", read) + return stream + + monkeypatch.setattr(drift_capture, "_windows_open", replacing) + with pytest.raises(ValueError, match="workspace_changed_during_capture"): + drift_capture.capture(repo, ["file.txt"]) + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows no-follow handle") +def test_windows_does_not_follow_raced_in_link_to_same_file(repo, windows, monkeypatch): + target = repo / "file.txt" + target.write_text("original") + saved = repo / "saved.txt" + opened = drift_capture._windows_open + + def replacing(path): + target.rename(saved) + try: + target.symlink_to(saved) + except OSError: + saved.rename(target) + pytest.skip("symbolic-link creation requires Windows privilege") + stream = opened(path) + monkeypatch.setattr( + stream, "read", lambda *args: pytest.fail("followed raced-in link") + ) + return stream + + monkeypatch.setattr(drift_capture, "_windows_open", replacing) + with pytest.raises((ValueError, OSError)): + drift_capture.capture(repo, ["file.txt"]) + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows directory sharing") +def test_windows_pins_parent_until_file_read_finishes(repo, windows, monkeypatch): + parent = repo / "nested" + parent.mkdir() + (parent / "file.txt").write_bytes(b"original") + opened = drift_capture._windows_open + + def check_parent(path): + with pytest.raises(PermissionError): + parent.rename(repo / "moved") + return opened(path) + + monkeypatch.setattr(drift_capture, "_windows_open", check_parent) + assert ( + drift_capture.capture(repo, ["nested/file.txt"])["files"]["nested/file.txt"][ + "text" + ] + == "original" + ) + parent.rename(repo / "moved") + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows junction race") +def test_windows_rejects_workspace_swap_between_lstat_and_open(repo, windows, monkeypatch): + parent = repo / "nested" + parent.mkdir() + (parent / "file.txt").write_text("inside") + outside = repo.parent / (repo.name + "-outside") + outside.mkdir() + (outside / "nested").mkdir() + (outside / "nested/file.txt").write_text("outside") + saved = repo.parent / (repo.name + "-moved") + original = windows.lstat + + def swapped(path): + observed = original(path) + if path == repo: + repo.rename(saved) + subprocess.run( + ["cmd", "/d", "/c", "mklink", "/J", str(repo), str(outside)], + check=True, + capture_output=True, + ) + return observed + + windows.lstat = swapped + monkeypatch.setattr( + drift_capture, "_windows_open", lambda path: pytest.fail("opened outside file") + ) + try: + with pytest.raises( + ValueError, match="scope_escapes_workspace|workspace_changed_during_capture" + ): + drift_capture.capture(repo, ["nested/file.txt"]) + finally: + if repo.is_junction(): + repo.rmdir() + if saved.exists(): + saved.rename(repo) + + +def test_posix_keeps_nofollow_open(repo, monkeypatch): + (repo / "file.txt").write_bytes(b"original\n") + platform = SimpleNamespace(**vars(os)) + platform.name = "posix" + platform.O_NOFOLLOW = getattr(os, "O_NOFOLLOW", 0x40000000) + calls = [] + + def opened(path, flags): + calls.append(flags) + actual = ( + flags if os.name != "nt" else (flags & ~platform.O_NOFOLLOW) | os.O_BINARY + ) + return os.open(path, actual) + + platform.open = opened + monkeypatch.setattr(drift_capture, "os", platform) + monkeypatch.setattr( + drift_capture, + "_windows_open", + lambda path: pytest.fail("Windows open on POSIX"), + ) + assert ( + drift_capture.stable_capture(repo, ["file.txt"])["files"]["file.txt"]["text"] + == "original\n" + ) + assert len(calls) == 2 and all(flags & platform.O_NOFOLLOW for flags in calls) diff --git a/packages/loopx-jev/tests/test_protocol.py b/packages/loopx-jev/tests/test_protocol.py index a5176f315b..a600d2efd6 100644 --- a/packages/loopx-jev/tests/test_protocol.py +++ b/packages/loopx-jev/tests/test_protocol.py @@ -2,22 +2,27 @@ import json import os -from pathlib import Path import subprocess import sys +from pathlib import Path import pytest from loopx_jev.config import load_config, strict_json from loopx_jev.progress import DOMAINS, decode_assessment from loopx_jev.protocol import validate_choice -from loopx_jev.transport import send, TransportFailure +from loopx_jev.transport import TransportFailure, send @pytest.mark.parametrize("code", ["unsupported_method", "invalid_request", "io_permission_denied"]) def test_manual_basis_preserves_only_the_typed_legacy_method_boundary(tmp_path, monkeypatch, code): - from loopx.control_plane.effect_runtime import EffectRuntimeRemoteError, EffectRuntimeRejected, EffectRuntimePermanentIOError from loopx_jev.runner import read_basis + from loopx.control_plane.effect_runtime import ( + EffectRuntimePermanentIOError, + EffectRuntimeRejected, + EffectRuntimeRemoteError, + ) + path = tmp_path / "basis.json" manifest = {"goal_id": "study", "objective": "Study a declared file", "acceptance": ["Observe its net change"], "evidence": [{"ref": "code.py"}]} @@ -134,6 +139,7 @@ def test_source_only_off_command_loads_no_transport(tmp_path): """ process = subprocess.run( [sys.executable, "-c", code], + check=False, cwd=tmp_path, capture_output=True, text=True, @@ -180,8 +186,15 @@ def test_only_actual_refresh_command_is_observed(args, expected): def test_unavailable_platform_file_primitives_are_explicit(tmp_path, monkeypatch): + from types import SimpleNamespace + + from loopx_jev import drift_capture from loopx_jev.drift_capture import capture - monkeypatch.delattr(os, "O_NOFOLLOW") + platform = SimpleNamespace(**vars(os)) + platform.name = "unsupported" + if hasattr(platform, "O_NOFOLLOW"): + del platform.O_NOFOLLOW + monkeypatch.setattr(drift_capture, "os", platform) with pytest.raises(ValueError, match="unsupported_capture_platform"): capture(tmp_path, ["file.txt"]) From be915c2b2466a7a52c4b3ab91c0c04d3b3aad477 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:34:30 +0800 Subject: [PATCH 2/9] fix(jev): enforce canonical Windows capture scope Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> --- packages/loopx-jev/DRIFT_SHADOW.md | 18 +++- packages/loopx-jev/DRIFT_SHADOW.zh-CN.md | 6 +- .../loopx-jev/src/loopx_jev/drift_capture.py | 31 +++++++ .../loopx-jev/tests/test_drift_capture.py | 88 ++++++++++++++++++- 4 files changed, 138 insertions(+), 5 deletions(-) diff --git a/packages/loopx-jev/DRIFT_SHADOW.md b/packages/loopx-jev/DRIFT_SHADOW.md index e70578b8f9..2cff77d9ec 100644 --- a/packages/loopx-jev/DRIFT_SHADOW.md +++ b/packages/loopx-jev/DRIFT_SHADOW.md @@ -59,6 +59,11 @@ baseline. Changes outside the list may still be valid Agent work. `no_delta` means no change in the observed material, not no progress on the whole task. Reading a test report does not run the test or independently certify its claim. +Git metadata (`.git` at any path component) is excluded. On Windows this rule +also applies to the normalized, workspace-relative path obtained from the opened +file handle, so case variants and 8.3 aliases cannot bypass it. Capture fails +closed if the canonical handle path cannot be obtained. + For example, selecting only a function's file may omit the helper it calls and the test that exercises it. A judgment from that packet cannot certify the full behavior. Include relevant tests, results and dependencies deliberately; if the @@ -112,9 +117,13 @@ certify model correctness or grant mutation authority. ## Run it -Use Python 3.11+ and the Node runtime required by the LoopX checkout. This -scoped collector targets POSIX file handling on Linux/macOS; Windows capture is -not qualified and unsupported file primitives produce an unavailable observation. +Use Python 3.11+ and the Node runtime required by the LoopX checkout. The +scoped collector uses POSIX no-follow file handling on Linux/macOS and native +handle, reparse-point, identity and canonical-path checks on Windows. Native +Windows tests cover local NTFS capture; ACL-restricted and network-share +behavior is not qualified. Unavailable file primitives or canonical paths +produce an unavailable observation. Directory handles stay pinned during capture +and can conflict with concurrent write/rename operations. The optional distribution is not part of the default LoopX wheel. Install from the source root into a fresh environment, then run the no-key checks: @@ -125,6 +134,9 @@ uv pip install --python .venv-jev/bin/python -e '.[test]' -e packages/loopx-jev .venv-jev/bin/loopx-jev drift --help ``` +On Windows, use `.venv-jev/Scripts/python.exe` and +`.venv-jev/Scripts/loopx-jev.exe` in place of the `bin` entrypoints above. + This implementation only observes task progress; other assessment directions and ranking code are not provided. Use `loopx_jev_drift_config_v0` and `minimum_label_probability`; old multi-direction pilot profiles are rejected rather than silently promoted. diff --git a/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md b/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md index 879654072b..8537e58055 100644 --- a/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md +++ b/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md @@ -25,6 +25,8 @@ 路径必须是文件,不是目录或通配符;最多选 32 个,并受下文的字节上限约束。初始化后清单固定;要变更范围,需显式新建观察器/预算并建立新基线。Agent 在清单外做的工作可能完全有效,`no_delta` 只表示所观察材料没有变化,不表示整个任务没有进展。读取测试报告也不等于执行测试或独立认证其中的声明。 +路径任何层级的 Git 元数据目录(`.git`)均被排除。在 Windows 上,同一规则也检查从已打开文件句柄取得的、相对工作区的规范路径,大小写变体和 8.3 别名不能绕过。无法取得规范句柄路径时,采集直接拒绝。 + 例如只选择函数所在文件,而漏掉被调用的 helper 和对应测试,评估就不能证明完整行为。应主动纳入相关测试、结果和依赖;必要材料装不下时,不能把部分材料包装成完整证据。 ## 绑定规范任务 criterion @@ -64,7 +66,7 @@ binding 固定 Goal/Agent/Todo 身份、contract 修订/摘要、任务语义 ## 操作方法 -使用 Python 3.11+ 和 LoopX 检出要求的 Node 运行时。采集器目前面向 Linux/macOS 的 POSIX 文件处理;Windows 采集未验证,缺少所需文件原语时记录观察不可用。此可选发行包不进入 LoopX 默认 wheel;从源码根目录在新环境中安装,再执行不需要 key 的集成测试: +使用 Python 3.11+ 和 LoopX 检出要求的 Node 运行时。采集器在 Linux/macOS 上沿用 POSIX no-follow 文件处理,在 Windows 上使用原生句柄、reparse point、身份和规范路径检查。原生 Windows 测试覆盖本地 NTFS 采集;受限 ACL 和网络共享场景尚未验证。缺少所需文件原语或无法取得规范路径时,记录观察不可用。采集期间目录句柄保持固定,可能与并发写入或重命名冲突。此可选发行包不进入 LoopX 默认 wheel;从源码根目录在新环境中安装,再执行不需要 key 的集成测试: ```bash uv venv .venv-jev @@ -73,6 +75,8 @@ uv pip install --python .venv-jev/bin/python -e '.[test]' -e packages/loopx-jev .venv-jev/bin/loopx-jev drift --help ``` +Windows 上将上述 `bin` 入口替换为 `.venv-jev/Scripts/python.exe` 和 `.venv-jev/Scripts/loopx-jev.exe`。 + 本切片只观察任务进展,不提供其他评估方向或排序代码。使用 `loopx_jev_drift_config_v0` 和 `minimum_label_probability`;旧多方向试点配置会被拒绝,不会静默提升。配置 key 本身不启用 shadow 或允许出站。失败时原 Agent 工作继续,不会自动启动独立 Agent 裁判。 为已有 Goal 创建被 Git 忽略的本地目录,复制 [config.shadow.json](examples/drift/config.shadow.json) 和 [basis.json](examples/drift/basis.json)。将示例 Goal id、目标、验收条件改为本次契约。可选 `evidence` 引用交付工作区中的常规文件,例如独立产生的测试报告;不要在配置或契约中填写密钥。默认 `allow_egress: false`,确认指定材料允许出站后再设为 true,并在**消费者的环境变量**中配置 `TYPESAFE_API_KEY`。 diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index b19193ff38..f88e2b77d5 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -132,9 +132,32 @@ def _windows_open(target: Path) -> BinaryIO: raise +def _windows_final_path(descriptor: int) -> Path: + import ctypes + import msvcrt + from ctypes import wintypes + + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + final_path = kernel.GetFinalPathNameByHandleW + final_path.argtypes = ( + wintypes.HANDLE, + wintypes.LPWSTR, + wintypes.DWORD, + wintypes.DWORD, + ) + final_path.restype = wintypes.DWORD + buffer = ctypes.create_unicode_buffer(32768) + # FILE_NAME_NORMALIZED | VOLUME_NAME_DOS keeps root and file names comparable. + length = final_path(msvcrt.get_osfhandle(descriptor), buffer, len(buffer), 0) + if not 0 < length < len(buffer): + raise ValueError("capture_path_unavailable") + return Path(buffer.value) + + def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: with ExitStack() as parents: current = repo + canonical_repo = repo for part in ("", *target.relative_to(repo).parts[:-1]): current = current / part try: @@ -145,6 +168,8 @@ def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: parents.callback(os.close, descriptor) if _windows_identity(os.fstat(descriptor), directory=True)[:2] != expected: raise ValueError("workspace_changed_during_capture") + if not part: + canonical_repo = _windows_final_path(descriptor) try: observed = os.lstat(target) except FileNotFoundError: @@ -153,6 +178,12 @@ def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: with _windows_open(target) as stream: if _windows_identity(os.fstat(stream.fileno())) != expected_file: raise ValueError("workspace_changed_during_capture") + canonical = _windows_final_path(stream.fileno()) + if not canonical.is_relative_to(canonical_repo): + raise ValueError("scope_escapes_workspace") + validate_paths( + canonical_repo, [canonical.relative_to(canonical_repo).as_posix()] + ) raw = stream.read(MAX_BYTES + 1) opened = os.fstat(stream.fileno()) if ( diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index 961e44b22b..4892959e5a 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -60,10 +60,20 @@ def windows(monkeypatch): monkeypatch.setattr(drift_capture, "os", platform) if os.name != "nt": # Native Windows exercises CreateFileW; other hosts simulate its IO boundary. + opened_paths = {} + + def descriptor(path, directory=False): + fd = os.open(path, os.O_RDONLY) + opened_paths[fd] = path.resolve() + return fd + monkeypatch.setattr( drift_capture, "_windows_descriptor", - lambda path, directory=False: os.open(path, os.O_RDONLY), + descriptor, + ) + monkeypatch.setattr( + drift_capture, "_windows_final_path", lambda fd: opened_paths[fd] ) return platform @@ -75,12 +85,88 @@ def test_windows_regular_file_without_nofollow(repo, windows): assert captured["files"]["missing.txt"] is None +def test_windows_stable_capture_allows_ctime_skew(repo, windows): + (repo / "file.txt").write_bytes(b"ordinary file\n") + lstat, fstat = windows.lstat, windows.fstat + # A stable file's lstat/fstat ctime can differ; identity must not depend on it. + windows.lstat = lambda path: stat_fields(lstat(path), st_ctime_ns=1) + windows.fstat = lambda fd: stat_fields(fstat(fd), st_ctime_ns=2) + assert ( + drift_capture.stable_capture(repo, ["file.txt"])["files"]["file.txt"]["text"] + == "ordinary file\n" + ) + + @pytest.mark.parametrize("name", [".GIT/config", ".gIt/config"]) def test_windows_rejects_case_insensitive_git_metadata(repo, windows, name): with pytest.raises(ValueError, match="invalid_scope_path"): drift_capture.capture(repo, [name]) +@pytest.fixture +def unread_windows_file(monkeypatch): + opened = drift_capture._windows_open + + def unread(path): + stream = opened(path) + monkeypatch.setattr( + stream, "read", lambda *args: pytest.fail("read excluded or unverified bytes") + ) + return stream + + monkeypatch.setattr(drift_capture, "_windows_open", unread) + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows path representations") +@pytest.mark.parametrize( + "representation", ["long", "case", "extended", "device", "short", "short_case"] +) +def test_windows_rejects_native_git_path_representations( + repo, windows, unread_windows_file, representation +): + if representation.startswith("short"): + result = subprocess.run( + ["fsutil", "file", "setshortname", str(repo / ".git"), "GIT~1"], + capture_output=True, + ) + if result.returncode: + pytest.skip( + "8.3 alias creation unavailable: " + + (result.stdout + result.stderr).decode(errors="replace").strip() + ) + name = { + "long": ".git/config", + "case": ".GiT/config", + "extended": f"//?/{repo.as_posix()}/.git/config", + "device": f"//./{repo.as_posix()}/.git/config", + "short": "GIT~1/config", + "short_case": "git~1/CONFIG", + }[representation] + assert os.path.samefile(repo / name, repo / ".git/config") + with pytest.raises(ValueError, match="invalid_scope_path"): + drift_capture.capture(repo, [name]) + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows final-path API") +@pytest.mark.parametrize("result", [0, 32768]) +def test_windows_fails_closed_without_final_path( + repo, windows, unread_windows_file, monkeypatch, result +): + import ctypes + + (repo / "file.txt").write_bytes(b"ordinary file") + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + + def unavailable(*args): + ctypes.set_last_error(5) + return result + + monkeypatch.setattr(kernel, "GetFinalPathNameByHandleW", unavailable) + monkeypatch.setattr(ctypes, "WinDLL", lambda *args, **kwargs: kernel) + with pytest.raises((ValueError, OSError)): + drift_capture.capture(repo, ["file.txt"]) + + @pytest.mark.parametrize("directory", [False, True]) def test_windows_rejects_symlink(repo, windows, directory): target = repo / "original" From 8e0d846193bb944ffe51c0c672a0e01222f9b2c4 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:20:15 +0800 Subject: [PATCH 3/9] fix(jev): validate canonical ancestors before accepting missing scope Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> --- packages/loopx-jev/DRIFT_SHADOW.md | 8 +- packages/loopx-jev/DRIFT_SHADOW.zh-CN.md | 2 +- .../loopx-jev/src/loopx_jev/drift_capture.py | 16 +- .../loopx-jev/tests/test_drift_capture.py | 291 ++++++++++++++++-- 4 files changed, 282 insertions(+), 35 deletions(-) diff --git a/packages/loopx-jev/DRIFT_SHADOW.md b/packages/loopx-jev/DRIFT_SHADOW.md index 2cff77d9ec..2300477321 100644 --- a/packages/loopx-jev/DRIFT_SHADOW.md +++ b/packages/loopx-jev/DRIFT_SHADOW.md @@ -61,8 +61,12 @@ Reading a test report does not run the test or independently certify its claim. Git metadata (`.git` at any path component) is excluded. On Windows this rule also applies to the normalized, workspace-relative path obtained from the opened -file handle, so case variants and 8.3 aliases cannot bypass it. Capture fails -closed if the canonical handle path cannot be obtained. +file handle, so case variants and 8.3 aliases cannot bypass it. Before accepting +a missing file or parent directory, capture applies the same rules to the +deepest existing, identity-checked ancestor's canonical path plus the remaining +components. Windows scope paths reject trailing dots/spaces, drive-relative +names and alternate-stream syntax, whose absent components cannot be normalized +by a handle. Capture fails closed if the canonical handle path cannot be obtained. For example, selecting only a function's file may omit the helper it calls and the test that exercises it. A judgment from that packet cannot certify the full diff --git a/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md b/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md index 8537e58055..03331be675 100644 --- a/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md +++ b/packages/loopx-jev/DRIFT_SHADOW.zh-CN.md @@ -25,7 +25,7 @@ 路径必须是文件,不是目录或通配符;最多选 32 个,并受下文的字节上限约束。初始化后清单固定;要变更范围,需显式新建观察器/预算并建立新基线。Agent 在清单外做的工作可能完全有效,`no_delta` 只表示所观察材料没有变化,不表示整个任务没有进展。读取测试报告也不等于执行测试或独立认证其中的声明。 -路径任何层级的 Git 元数据目录(`.git`)均被排除。在 Windows 上,同一规则也检查从已打开文件句柄取得的、相对工作区的规范路径,大小写变体和 8.3 别名不能绕过。无法取得规范句柄路径时,采集直接拒绝。 +路径任何层级的 Git 元数据目录(`.git`)均被排除。在 Windows 上,同一规则也检查从已打开文件句柄取得的、相对工作区的规范路径,大小写变体和 8.3 别名不能绕过。将文件或中间目录记为缺失之前,采集会把最深的已有、身份已核验祖先的规范路径与全部剩余组件拼接,再应用相同规则。Windows 范围路径拒绝尾随点或空格、盘符相对路径及备用数据流写法,因为不存在的组件无法通过句柄规范化。无法取得规范句柄路径时,采集直接拒绝。 例如只选择函数所在文件,而漏掉被调用的 helper 和对应测试,评估就不能证明完整行为。应主动纳入相关测试、结果和依赖;必要材料装不下时,不能把部分材料包装成完整证据。 diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index f88e2b77d5..78a5f0afd3 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -55,6 +55,10 @@ def validate_paths(repo: Path, paths: list[str]) -> list[str]: or path.is_absolute() or ".." in path.parts or ".git" in parts + or ( + os.name == "nt" + and any(":" in part or part.endswith((" ", ".")) for part in path.parts) + ) or name != path.as_posix() or any(ord(char) < 32 for char in name) ): @@ -163,13 +167,23 @@ def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: try: expected = _windows_identity(os.lstat(current), directory=True)[:2] except FileNotFoundError: + if not part: + raise ValueError("capture_path_unavailable") from None return None descriptor = _windows_descriptor(current, directory=True) parents.callback(os.close, descriptor) if _windows_identity(os.fstat(descriptor), directory=True)[:2] != expected: raise ValueError("workspace_changed_during_capture") + canonical = _windows_final_path(descriptor) if not part: - canonical_repo = _windows_final_path(descriptor) + canonical_repo = canonical + # Validate the known ancestor plus the full suffix before accepting absence. + canonical = canonical / target.relative_to(current) + if not canonical.is_relative_to(canonical_repo): + raise ValueError("scope_escapes_workspace") + validate_paths( + canonical_repo, [canonical.relative_to(canonical_repo).as_posix()] + ) try: observed = os.lstat(target) except FileNotFoundError: diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index 4892959e5a..8221734096 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -1,5 +1,6 @@ """File identity and no-follow boundaries of scoped workspace capture.""" +import json import os import stat import subprocess @@ -110,7 +111,9 @@ def unread_windows_file(monkeypatch): def unread(path): stream = opened(path) monkeypatch.setattr( - stream, "read", lambda *args: pytest.fail("read excluded or unverified bytes") + stream, + "read", + lambda *args: pytest.fail("read excluded or unverified bytes"), ) return stream @@ -118,53 +121,274 @@ def unread(path): @pytest.mark.skipif(os.name != "nt", reason="native Windows path representations") +@pytest.mark.parametrize("nested", [False, True]) +@pytest.mark.parametrize("leaf", ["config", "missing.txt", "absent/missing.txt"]) @pytest.mark.parametrize( "representation", ["long", "case", "extended", "device", "short", "short_case"] ) def test_windows_rejects_native_git_path_representations( - repo, windows, unread_windows_file, representation + repo, windows, unread_windows_file, representation, leaf, nested ): + metadata = repo / "ordinary/.git" if nested else repo / ".git" + if nested: + metadata.mkdir(parents=True) + (metadata / "config").write_bytes(b"excluded metadata") if representation.startswith("short"): - result = subprocess.run( - ["fsutil", "file", "setshortname", str(repo / ".git"), "GIT~1"], - capture_output=True, - ) - if result.returncode: - pytest.skip( - "8.3 alias creation unavailable: " - + (result.stdout + result.stderr).decode(errors="replace").strip() - ) - name = { - "long": ".git/config", - "case": ".GiT/config", - "extended": f"//?/{repo.as_posix()}/.git/config", - "device": f"//./{repo.as_posix()}/.git/config", - "short": "GIT~1/config", - "short_case": "git~1/CONFIG", + set_short_name(metadata, "GIT~1") + prefix = "ordinary/" if nested else "" + parent = { + "long": f"{prefix}.git", + "case": f"{prefix}.GiT", + "extended": f"//?/{metadata.as_posix()}", + "device": f"//./{metadata.as_posix()}", + "short": f"{prefix}GIT~1", + "short_case": f"{prefix}git~1", }[representation] - assert os.path.samefile(repo / name, repo / ".git/config") + assert os.path.samefile(repo / parent, metadata) + name = f"{parent}/{leaf.upper() if representation == 'short_case' else leaf}" + assert (repo / name).exists() == (leaf == "config") with pytest.raises(ValueError, match="invalid_scope_path"): drift_capture.capture(repo, [name]) +@pytest.mark.skipif(os.name != "nt", reason="native Windows legacy spellings") +@pytest.mark.parametrize("parent", [".git.", ".git ", ".git::$INDEX_ALLOCATION"]) +@pytest.mark.parametrize("leaf", ["config", "missing.txt", "absent/missing.txt"]) +def test_windows_rejects_git_legacy_spellings( + repo, windows, unread_windows_file, parent, leaf +): + with pytest.raises((ValueError, OSError)): + drift_capture.capture(repo, [f"{parent}/{leaf}"]) + + +@pytest.mark.parametrize( + "name", + [ + "ordinary/missing./file.txt", + "ordinary/missing /file.txt", + "ordinary/file.txt:stream", + "C:ordinary/missing.txt", + ], +) +def test_windows_rejects_ambiguous_missing_components(repo, windows, name): + (repo / "ordinary").mkdir() + with pytest.raises(ValueError, match="invalid_scope_path"): + drift_capture.capture(repo, [name]) + + +def set_short_name(path, alias): + result = subprocess.run( + ["fsutil", "file", "setshortname", str(path), alias], capture_output=True + ) + if result.returncode: + pytest.skip( + "8.3 alias creation unavailable: " + + (result.stdout + result.stderr).decode(errors="replace").strip() + ) + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows directory aliases") +@pytest.mark.parametrize("leaf", ["missing.txt", "absent/missing.txt"]) +def test_windows_missing_git_alias_cannot_initialize(repo, windows, leaf, capsys): + from loopx_jev.cli import main + + set_short_name(repo / ".git", "GIT~1") + assert os.path.samefile(repo / "GIT~1", repo / ".git") + config, basis, observer = ( + repo / "config.json", + repo / "basis.json", + repo / "observer", + ) + config.write_text( + json.dumps( + { + "schema_version": "loopx_jev_drift_config_v0", + "mode": "shadow", + "scenarios": ["progress_review"], + "model": "fixture-v1", + "allow_egress": False, + } + ) + ) + basis.write_text( + json.dumps( + { + "goal_id": "alias-test", + "objective": "Observe ordinary future files", + "acceptance": ["New ordinary files produce evidence"], + "evidence": [], + } + ) + ) + assert ( + main( + [ + "drift", + "init", + "--state-dir", + str(observer), + "--workspace", + str(repo), + "--basis", + str(basis), + "--config", + str(config), + "--path", + f"GIT~1/{leaf}", + ] + ) + == 2 + ) + assert "unavailable" in capsys.readouterr().err + assert not observer.exists() + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows directory aliases") +@pytest.mark.parametrize("leaf", ["missing.txt", "absent/missing.txt"]) +def test_windows_missing_ordinary_alias_captures_when_created(repo, windows, leaf): + parent = repo / "ordinary directory" + parent.mkdir() + set_short_name(parent, "PLAIN~1") + name = f"plain~1/{leaf}" + assert os.path.samefile(repo / "plain~1", parent) + before = drift_capture.stable_capture(repo, [name]) + assert before["files"][name] is None + target = parent / leaf + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(b"ordinary new evidence\n") + after = drift_capture.stable_capture(repo, [name]) + assert after["files"][name]["text"] == "ordinary new evidence\n" + assert "ordinary new evidence" in drift_capture.delta(before, after) + + +def test_windows_regular_hard_link_stays_capturable(repo, windows): + original, link = repo / "file.txt", repo / "link.txt" + original.write_bytes(b"ordinary linked file\n") + link.hardlink_to(original) + assert os.path.samefile(original, link) + files = drift_capture.stable_capture(repo, ["file.txt", "link.txt"])["files"] + assert ( + files["file.txt"]["text"] + == files["link.txt"]["text"] + == "ordinary linked file\n" + ) + + @pytest.mark.skipif(os.name != "nt", reason="native Windows final-path API") @pytest.mark.parametrize("result", [0, 32768]) +@pytest.mark.parametrize( + "missing,query", [(False, 1), (False, 2), (False, 3), (True, 1), (True, 2)] +) def test_windows_fails_closed_without_final_path( - repo, windows, unread_windows_file, monkeypatch, result + repo, windows, unread_windows_file, monkeypatch, result, missing, query ): import ctypes + from ctypes import wintypes - (repo / "file.txt").write_bytes(b"ordinary file") + (repo / "nested").mkdir() + if not missing: + (repo / "nested/file.txt").write_bytes(b"ordinary file") kernel = ctypes.WinDLL("kernel32", use_last_error=True) + final_path = kernel.GetFinalPathNameByHandleW + final_path.argtypes = ( + wintypes.HANDLE, + wintypes.LPWSTR, + wintypes.DWORD, + wintypes.DWORD, + ) + final_path.restype = wintypes.DWORD + calls = 0 def unavailable(*args): - ctypes.set_last_error(5) - return result + nonlocal calls + calls += 1 + if calls == query: + ctypes.set_last_error(5) + return result + return final_path(*args) monkeypatch.setattr(kernel, "GetFinalPathNameByHandleW", unavailable) monkeypatch.setattr(ctypes, "WinDLL", lambda *args, **kwargs: kernel) - with pytest.raises((ValueError, OSError)): - drift_capture.capture(repo, ["file.txt"]) + with pytest.raises(ValueError, match="capture_path_unavailable"): + drift_capture.capture(repo, ["nested/file.txt"]) + assert calls == query + + +def test_windows_missing_workspace_is_not_an_absent_scoped_file(repo, windows): + saved = repo.with_name(repo.name + "-moved") + original = windows.lstat + + def vanished(path): + if path == repo: + repo.rename(saved) + return original(path) + + windows.lstat = vanished + try: + with pytest.raises(ValueError, match="capture_path_unavailable"): + drift_capture._windows_capture(repo, repo / "missing.txt") + finally: + saved.rename(repo) + + +@pytest.mark.parametrize("name", ["ordinary", "ordinary/missing.txt"]) +def test_windows_missing_path_requires_directory_ancestors_and_file_leaf( + repo, windows, name +): + parent = repo / "ordinary" + parent.mkdir() if name == "ordinary" else parent.write_bytes(b"regular file") + with pytest.raises(ValueError, match="scope_requires_regular_files"): + drift_capture.capture(repo, [name]) + + +def test_windows_missing_path_rejects_outside_canonical_ancestor( + repo, windows, monkeypatch +): + parent = repo / "nested" + parent.mkdir() + identity = os.stat(parent).st_ino + final_path = drift_capture._windows_final_path + monkeypatch.setattr( + drift_capture, + "_windows_final_path", + lambda fd: repo.parent if os.fstat(fd).st_ino == identity else final_path(fd), + ) + with pytest.raises(ValueError, match="scope_escapes_workspace"): + drift_capture.capture(repo, ["nested/absent/file.txt"]) + + +@pytest.mark.skipif(os.name != "nt", reason="native Windows directory sharing") +def test_windows_missing_path_fails_closed_on_directory_sharing_conflict(repo, windows): + import ctypes + from ctypes import wintypes + + parent = repo / "nested" + parent.mkdir() + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + create = kernel.CreateFileW + create.argtypes = ( + wintypes.LPCWSTR, + wintypes.DWORD, + wintypes.DWORD, + wintypes.LPVOID, + wintypes.DWORD, + wintypes.DWORD, + wintypes.HANDLE, + ) + create.restype = wintypes.HANDLE + kernel.CloseHandle.argtypes = (wintypes.HANDLE,) + kernel.CloseHandle.restype = wintypes.BOOL + # A real no-share handle makes the ancestor inaccessible to the collector. + handle = create("\\\\?\\" + str(parent), 0x80000000, 0, None, 3, 0x02000000, None) + assert handle != wintypes.HANDLE(-1).value + try: + with pytest.raises(PermissionError): + drift_capture.capture(repo, ["nested/absent/file.txt"]) + finally: + kernel.CloseHandle(handle) + assert drift_capture.capture(repo, ["nested/absent/file.txt"])["files"] == { + "nested/absent/file.txt": None, + } @pytest.mark.parametrize("directory", [False, True]) @@ -207,7 +431,8 @@ def test_windows_rejects_reparse_point(repo, windows, component): @pytest.mark.skipif(os.name != "nt", reason="native Windows junction") -def test_windows_rejects_real_junction(repo, windows): +@pytest.mark.parametrize("leaf", ["file.txt", "missing.txt", "absent/missing.txt"]) +def test_windows_rejects_real_junction(repo, windows, leaf): target = repo / "directory" target.mkdir() (target / "file.txt").write_text("original") @@ -221,7 +446,7 @@ def test_windows_rejects_real_junction(repo, windows): try: assert link.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT with pytest.raises(ValueError, match="scope_escapes_workspace"): - drift_capture.capture(repo, ["junction/file.txt"]) + drift_capture.capture(repo, [f"junction/{leaf}"]) finally: link.rmdir() @@ -249,12 +474,13 @@ def unread(path): @pytest.mark.parametrize("field", ["st_dev", "st_ino"]) @pytest.mark.parametrize("boundary", ["lstat", "fstat"]) -def test_windows_fails_closed_without_identity(repo, windows, field, boundary): +@pytest.mark.parametrize("name", ["file.txt", "absent/file.txt"]) +def test_windows_fails_closed_without_identity(repo, windows, field, boundary, name): (repo / "file.txt").write_text("original") original = getattr(windows, boundary) setattr(windows, boundary, lambda value: stat_fields(original(value), **{field: 0})) with pytest.raises(ValueError, match="capture_identity_unavailable"): - drift_capture.capture(repo, ["file.txt"]) + drift_capture.capture(repo, [name]) def test_windows_rejects_mutation_during_read(repo, windows, monkeypatch): @@ -327,7 +553,10 @@ def check_parent(path): @pytest.mark.skipif(os.name != "nt", reason="native Windows junction race") -def test_windows_rejects_workspace_swap_between_lstat_and_open(repo, windows, monkeypatch): +@pytest.mark.parametrize("leaf", ["file.txt", "absent/file.txt"]) +def test_windows_rejects_workspace_swap_between_lstat_and_open( + repo, windows, monkeypatch, leaf +): parent = repo / "nested" parent.mkdir() (parent / "file.txt").write_text("inside") @@ -357,7 +586,7 @@ def swapped(path): with pytest.raises( ValueError, match="scope_escapes_workspace|workspace_changed_during_capture" ): - drift_capture.capture(repo, ["nested/file.txt"]) + drift_capture.capture(repo, [f"nested/{leaf}"]) finally: if repo.is_junction(): repo.rmdir() From c18ffd5d6999f1e58509077885c16dcd91420919 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:15:24 +0800 Subject: [PATCH 4/9] fix(jev): read the index through the same case alias as the captured file On a case-insensitive workspace a scoped path such as RECORD.txt opens the tracked record.txt, but the literal index query matched nothing, so an index-only change was reported as no_delta. Both bounded index reads now use a case-insensitive literal pathspec when core.ignorecase is set. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude --- .../loopx-jev/src/loopx_jev/drift_capture.py | 28 ++++++++++++--- .../loopx-jev/tests/test_drift_capture.py | 34 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index 78a5f0afd3..b846c7ae06 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -208,6 +208,28 @@ def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: return raw, opened.st_mode & 0o111 +def _index(repo: Path, paths: list[str]) -> str: + # A case-insensitive workspace opens RECORD.txt as the tracked record.txt; + # read that entry the same way so its staged changes are not missed. + try: + icase = ( + git(repo, "config", "--type=bool", "--get", "core.ignorecase").strip() + == "true" + ) + except ValueError: # unset: Git matches paths exactly + icase = False + if icase: + return git( + repo, + "ls-files", + "--stage", + "-z", + "--", + *(f":(icase,literal){name}" for name in paths), + ) + return git(repo, "--literal-pathspecs", "ls-files", "--stage", "-z", "--", *paths) + + def capture(repo: Path, paths: list[str]) -> dict[str, Any]: windows = os.name == "nt" and not hasattr(os, "O_NOFOLLOW") if not windows and not hasattr(os, "O_NOFOLLOW"): @@ -217,7 +239,7 @@ def capture(repo: Path, paths: list[str]) -> dict[str, Any]: if Path(git(repo, "rev-parse", "--show-toplevel").strip()).resolve() != repo: raise ValueError("workspace_must_be_git_root") before = git(repo, "rev-parse", "HEAD").strip() - index = git(repo, "--literal-pathspecs", "ls-files", "--stage", "-z", "--", *paths) + index = _index(repo, paths) files: dict[str, Any] = {} size = 0 for name in paths: @@ -246,9 +268,7 @@ def capture(repo: Path, paths: list[str]) -> dict[str, Any]: if size > MAX_BYTES or b"\0" in raw: raise ValueError("oversized_or_binary_scope") files[name] = {"text": raw.decode("utf-8"), "executable": bool(mode)} - if before != git(repo, "rev-parse", "HEAD").strip() or index != git( - repo, "--literal-pathspecs", "ls-files", "--stage", "-z", "--", *paths - ): + if before != git(repo, "rev-parse", "HEAD").strip() or index != _index(repo, paths): raise ValueError("workspace_changed_during_capture") return { "head": before, diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index 8221734096..697b73fc95 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -594,6 +594,40 @@ def swapped(path): saved.rename(repo) +@pytest.mark.parametrize("alias", ["record.txt", "RECORD.txt"]) +def test_index_only_change_is_seen_through_case_alias(repo, alias): + def git(*args): + return subprocess.run( + ["git", "-C", str(repo), *args], check=True, capture_output=True, text=True + ).stdout + + if ( + git( + "config", "--type=bool", "--default=false", "--get", "core.ignorecase" + ).strip() + != "true" + ): + pytest.skip("requires a case-insensitive workspace") + if os.name != "nt" and not hasattr(os, "O_NOFOLLOW"): + pytest.skip("unsupported capture platform") + (repo / "record.txt").write_bytes(b"original\n") + git("add", "record.txt") + before = drift_capture.stable_capture(repo, [alias]) + assert before["files"][alias]["text"] == "original\n" + # Stage different content, then restore the working file: only the index changed. + (repo / "record.txt").write_bytes(b"staged\n") + git("add", "record.txt") + (repo / "record.txt").write_bytes(b"original\n") + after = drift_capture.stable_capture(repo, [alias]) + assert after["content_digest"] == before["content_digest"] + assert after["index_digest"] != before["index_digest"] + # An untracked or absent neighbour keeps its ordinary empty index evidence. + assert ( + drift_capture.stable_capture(repo, ["missing.txt"])["files"]["missing.txt"] + is None + ) + + def test_posix_keeps_nofollow_open(repo, monkeypatch): (repo / "file.txt").write_bytes(b"original\n") platform = SimpleNamespace(**vars(os)) From 2824e9123ca784253e7573fdac14a1baef52ec48 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:06:26 +0800 Subject: [PATCH 5/9] fix(jev): find the index entry for the captured file by identity The case-insensitive pathspec only folds ASCII and is reinterpreted by an inherited GIT_LITERAL_PATHSPECS, so index-only changes were still missed. Isolate pathspec environment for every Git read, and on a case-insensitive workspace resolve an alias through the directory's index entries, matching the folded name and, when both exist, the same file. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude --- .../loopx-jev/src/loopx_jev/drift_capture.py | 136 ++++++++++++++++-- .../loopx-jev/tests/test_drift_capture.py | 93 +++++++++--- 2 files changed, 200 insertions(+), 29 deletions(-) diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index b846c7ae06..0c1888cce8 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -8,6 +8,8 @@ import stat import subprocess import tempfile +import unicodedata +from collections.abc import Iterator from contextlib import ExitStack from pathlib import Path from typing import Any, BinaryIO @@ -22,6 +24,21 @@ def digest(value: Any) -> str: return hashlib.sha256(request_bytes(value)).hexdigest() +# Inherited pathspec modes would reinterpret the explicit pathspecs used below. +_PATHSPEC_ENV = ( + "GIT_LITERAL_PATHSPECS", + "GIT_GLOB_PATHSPECS", + "GIT_NOGLOB_PATHSPECS", + "GIT_ICASE_PATHSPECS", +) + + +def _git_env() -> dict[str, str]: + env = {key: value for key, value in os.environ.items() if key not in _PATHSPEC_ENV} + env["GIT_TERMINAL_PROMPT"] = "0" + return env + + def git(repo: Path, *args: str) -> str: # File-backed output avoids accumulating an unbounded Git response in RAM. with tempfile.TemporaryFile() as output: @@ -31,7 +48,7 @@ def git(repo: Path, *args: str) -> str: stderr=subprocess.DEVNULL, timeout=5, check=False, - env={**os.environ, "GIT_TERMINAL_PROMPT": "0"}, + env=_git_env(), ) output.seek(0) raw = output.read(131073) @@ -208,9 +225,83 @@ def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: return raw, opened.st_mode & 0o111 +def _name_key(name: str) -> str: + # One component, compared the way a case-insensitive file system resolves it. + return unicodedata.normalize("NFC", unicodedata.normalize("NFC", name).lower()) + + +def _same_file(left: Path, right: Path) -> bool: + try: + return os.path.samefile(left, right) + except OSError: + return False + + +def _disk_parent(repo: Path, name: str) -> str: + """Spell the existing leading directories of name as their parents list them.""" + parts = name.split("/")[:-1] + current, spelled = repo, [] + for position, part in enumerate(parts): + try: + names = os.listdir(current) + except OSError: + return "/".join([*spelled, *parts[position:]]) + if part not in names: + if not os.path.lexists(current / part): + return "/".join([*spelled, *parts[position:]]) + # Prefer names that fold to the same key; a short-name alias has none. + folded = [ + listed for listed in names if _name_key(listed) == _name_key(part) + ] + matches = [ + listed + for listed in folded or names + if _same_file(current / listed, current / part) + ] + # An unresolved spelling stays as given; path validity is judged by capture. + if len(matches) == 1: + part = matches[0] + spelled.append(part) + current = current / part + return "/".join(spelled) + + +def _index_children(repo: Path, parent: str) -> Iterator[str]: + """Yield the index records directly inside parent without holding the listing.""" + escaped = "".join("\\" + char if char in "\\*?[" else char for char in parent) + with tempfile.TemporaryFile() as output: + result = subprocess.run( + [ + "git", + "--no-optional-locks", + "-C", + str(repo), + "ls-files", + "--stage", + "-z", + "--", + ":(glob,icase)" + (escaped + "/*" if escaped else "*"), + ], + stdout=output, + stderr=subprocess.DEVNULL, + timeout=5, + check=False, + env=_git_env(), + ) + if result.returncode: + raise ValueError("git_evidence_unavailable") + output.seek(0) + pending = b"" + while chunk := output.read(65536): + *complete, pending = (pending + chunk).split(b"\0") + for record in complete: + yield record.decode("utf-8") + + def _index(repo: Path, paths: list[str]) -> str: - # A case-insensitive workspace opens RECORD.txt as the tracked record.txt; - # read that entry the same way so its staged changes are not missed. + literal = git( + repo, "--literal-pathspecs", "ls-files", "--stage", "-z", "--", *paths + ) try: icase = ( git(repo, "config", "--type=bool", "--get", "core.ignorecase").strip() @@ -218,16 +309,35 @@ def _index(repo: Path, paths: list[str]) -> str: ) except ValueError: # unset: Git matches paths exactly icase = False - if icase: - return git( - repo, - "ls-files", - "--stage", - "-z", - "--", - *(f":(icase,literal){name}" for name in paths), - ) - return git(repo, "--literal-pathspecs", "ls-files", "--stage", "-z", "--", *paths) + if not icase: + return literal + # A case-insensitive workspace opens an alias such as RECORD.txt as the tracked + # record.txt. Git matches neither a literal alias nor, beyond ASCII, an icase + # one, so find the entry for the same file among its directory's index entries. + records = {record for record in literal.split("\0") if record} + found = {record.split("\t", 1)[1] for record in records} + for name in paths: + if name in found: + continue + target = repo / name + key = _name_key(name.rsplit("/", 1)[-1]) + for record in _index_children(repo, _disk_parent(repo, name)): + path = record.split("\t", 1)[1] + if _name_key(path.rsplit("/", 1)[-1]) != key: + continue + entry = repo / path + # Two existing files that only share a key are different files. + if ( + os.path.lexists(target) + and os.path.lexists(entry) + and not _same_file(entry, target) + ): + continue + records.add(record) + return "".join( + record + "\0" + for record in sorted(records, key=lambda item: (item.split("\t", 1)[1], item)) + ) def capture(repo: Path, paths: list[str]) -> dict[str, Any]: diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index 697b73fc95..ee0214540e 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -594,37 +594,98 @@ def swapped(path): saved.rename(repo) -@pytest.mark.parametrize("alias", ["record.txt", "RECORD.txt"]) -def test_index_only_change_is_seen_through_case_alias(repo, alias): - def git(*args): - return subprocess.run( - ["git", "-C", str(repo), *args], check=True, capture_output=True, text=True - ).stdout +def _git(repo, *args): + return subprocess.run( + ["git", "-C", str(repo), *args], check=True, capture_output=True, text=True + ).stdout + +@pytest.fixture +def icase_repo(repo): if ( - git( - "config", "--type=bool", "--default=false", "--get", "core.ignorecase" + _git( + repo, "config", "--type=bool", "--default=false", "--get", "core.ignorecase" ).strip() != "true" ): pytest.skip("requires a case-insensitive workspace") if os.name != "nt" and not hasattr(os, "O_NOFOLLOW"): pytest.skip("unsupported capture platform") - (repo / "record.txt").write_bytes(b"original\n") - git("add", "record.txt") + return repo + + +def _stage(repo, name, text): + target = repo / name + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(text) + _git(repo, "add", "--", name) + + +@pytest.mark.parametrize("literal_env", [False, True], ids=["clean-env", "literal-env"]) +@pytest.mark.parametrize( + ("tracked", "alias"), + [ + ("record.txt", "record.txt"), + ("record.txt", "RECORD.txt"), + ("caf\u00e9.txt", "CAF\u00c9.txt"), + ("dir/caf\u00e9.txt", "DIR/CAF\u00c9.txt"), + ], + ids=["exact", "ascii-alias", "non-ascii-alias", "aliased-directory"], +) +def test_index_only_change_is_seen_through_any_spelling( + icase_repo, monkeypatch, literal_env, tracked, alias +): + repo = icase_repo + if literal_env: + # An inherited pathspec mode must not reinterpret the capture's own queries. + monkeypatch.setenv("GIT_LITERAL_PATHSPECS", "1") + _stage(repo, tracked, b"original\n") before = drift_capture.stable_capture(repo, [alias]) assert before["files"][alias]["text"] == "original\n" # Stage different content, then restore the working file: only the index changed. - (repo / "record.txt").write_bytes(b"staged\n") - git("add", "record.txt") - (repo / "record.txt").write_bytes(b"original\n") + _stage(repo, tracked, b"staged\n") + (repo / tracked).write_bytes(b"original\n") after = drift_capture.stable_capture(repo, [alias]) assert after["content_digest"] == before["content_digest"] assert after["index_digest"] != before["index_digest"] - # An untracked or absent neighbour keeps its ordinary empty index evidence. + + +@pytest.mark.parametrize( + ("tracked", "alias"), + [("record.txt", "RECORD.txt"), ("caf\u00e9.txt", "CAF\u00c9.txt")], + ids=["ascii-alias", "non-ascii-alias"], +) +def test_deleted_but_tracked_file_keeps_index_evidence_through_alias( + icase_repo, tracked, alias +): + repo = icase_repo + empty = drift_capture.stable_capture(repo, [alias]) + assert empty["files"][alias] is None + _stage(repo, tracked, b"first\n") + (repo / tracked).unlink() + first = drift_capture.stable_capture(repo, [alias]) + assert first["files"][alias] is None + assert first["index_digest"] != empty["index_digest"] + _stage(repo, tracked, b"second\n") + (repo / tracked).unlink() + second = drift_capture.stable_capture(repo, [alias]) + assert second["index_digest"] != first["index_digest"] + + +def test_distinct_and_missing_files_keep_empty_index_evidence(icase_repo): + repo = icase_repo + empty = drift_capture.stable_capture(repo, ["missing.txt"]) + assert empty["files"]["missing.txt"] is None + # A tracked neighbour with another name is not evidence for the selected file. + _stage(repo, "record.txt", b"tracked\n") + _stage(repo, "dir/record.txt", b"tracked elsewhere\n") + for name in ("missing.txt", "new/missing.txt", "records.txt"): + captured = drift_capture.stable_capture(repo, [name]) + assert captured["index_digest"] == empty["index_digest"], name + (repo / "untracked.txt").write_bytes(b"new\n") assert ( - drift_capture.stable_capture(repo, ["missing.txt"])["files"]["missing.txt"] - is None + drift_capture.stable_capture(repo, ["untracked.txt"])["index_digest"] + == empty["index_digest"] ) From 717ecf7ca681ee2e9f679d7d7f42e4bf4cae5b01 Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:53:08 +0800 Subject: [PATCH 6/9] fix(jev): match an existing file's index entry by identity Lower-case keys filtered candidates before the identity check, and the directory glob used Git's ASCII-only folding, so final sigma, sharp s and a parent renamed by non-ASCII case still lost staged changes. Stream the index once: an existing file matches entries in the same directory by identity, a missing file matches by Unicode caseless name. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- .../loopx-jev/src/loopx_jev/drift_capture.py | 75 ++++++++++++------- .../loopx-jev/tests/test_drift_capture.py | 46 ++++++++++++ 2 files changed, 92 insertions(+), 29 deletions(-) diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index 0c1888cce8..0b48f62a0d 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -225,9 +225,9 @@ def _windows_capture(repo: Path, target: Path) -> tuple[bytes, int] | None: return raw, opened.st_mode & 0o111 -def _name_key(name: str) -> str: - # One component, compared the way a case-insensitive file system resolves it. - return unicodedata.normalize("NFC", unicodedata.normalize("NFC", name).lower()) +def _caseless(text: str) -> str: + # Unicode caseless matching (full case folding), as case-insensitive volumes compare names. + return unicodedata.normalize("NFD", unicodedata.normalize("NFD", text).casefold()) def _same_file(left: Path, right: Path) -> bool: @@ -251,7 +251,7 @@ def _disk_parent(repo: Path, name: str) -> str: return "/".join([*spelled, *parts[position:]]) # Prefer names that fold to the same key; a short-name alias has none. folded = [ - listed for listed in names if _name_key(listed) == _name_key(part) + listed for listed in names if _caseless(listed) == _caseless(part) ] matches = [ listed @@ -266,9 +266,8 @@ def _disk_parent(repo: Path, name: str) -> str: return "/".join(spelled) -def _index_children(repo: Path, parent: str) -> Iterator[str]: - """Yield the index records directly inside parent without holding the listing.""" - escaped = "".join("\\" + char if char in "\\*?[" else char for char in parent) +def _index_records(repo: Path) -> Iterator[str]: + """Yield every index record from a file-backed read, without holding the listing.""" with tempfile.TemporaryFile() as output: result = subprocess.run( [ @@ -279,8 +278,6 @@ def _index_children(repo: Path, parent: str) -> Iterator[str]: "ls-files", "--stage", "-z", - "--", - ":(glob,icase)" + (escaped + "/*" if escaped else "*"), ], stdout=output, stderr=subprocess.DEVNULL, @@ -309,31 +306,51 @@ def _index(repo: Path, paths: list[str]) -> str: ) except ValueError: # unset: Git matches paths exactly icase = False - if not icase: - return literal - # A case-insensitive workspace opens an alias such as RECORD.txt as the tracked - # record.txt. Git matches neither a literal alias nor, beyond ASCII, an icase - # one, so find the entry for the same file among its directory's index entries. records = {record for record in literal.split("\0") if record} found = {record.split("\t", 1)[1] for record in records} - for name in paths: - if name in found: - continue - target = repo / name - key = _name_key(name.rsplit("/", 1)[-1]) - for record in _index_children(repo, _disk_parent(repo, name)): + pending = [name for name in paths if name not in found] + if icase and pending: + # A case-insensitive workspace opens an alias such as RECORD.txt as the + # tracked record.txt, and Git's own case folding differs from the file + # system's. An existing file is matched to its index entry by identity: + # only entries in the same directory are compared. A missing file has no + # identity, so its entry is matched by caseless name; every entry that + # could be it is kept, so an index change is never reported as no_delta. + existing = [name for name in pending if os.path.lexists(repo / name)] + missing: dict[str, list[str]] = {} + for name in pending: + if name in existing: + continue + parent = _disk_parent(repo, name) + leaf = name.rsplit("/", 1)[-1] + missing.setdefault( + _caseless(f"{parent}/{leaf}" if parent else leaf), [] + ).append(name) + same_directory: dict[str, list[str]] = {} + for record in _index_records(repo): path = record.split("\t", 1)[1] - if _name_key(path.rsplit("/", 1)[-1]) != key: + if path in found: continue - entry = repo / path - # Two existing files that only share a key are different files. - if ( - os.path.lexists(target) - and os.path.lexists(entry) - and not _same_file(entry, target) - ): + if _caseless(path) in missing: + records.add(record) + continue + if not existing: continue - records.add(record) + directory = path.rsplit("/", 1)[0] if "/" in path else "" + if directory not in same_directory: + folder = repo / directory if directory else repo + same_directory[directory] = [ + name + for name in existing + if _same_file(folder, (repo / name).parent) + ] + if any( + _same_file(repo / path, repo / name) + for name in same_directory[directory] + ): + records.add(record) + elif not icase: + return literal return "".join( record + "\0" for record in sorted(records, key=lambda item: (item.split("\t", 1)[1], item)) diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index ee0214540e..61c1cc8567 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -689,6 +689,52 @@ def test_distinct_and_missing_files_keep_empty_index_evidence(icase_repo): ) +def _index_blob_change(repo, tracked, text): + """Point the index entry at a new blob without touching the working file.""" + blob = ( + subprocess.run( + ["git", "-C", str(repo), "hash-object", "-w", "--stdin"], + input=text, + check=True, + capture_output=True, + ) + .stdout.decode() + .strip() + ) + _git(repo, "update-index", "--cacheinfo", f"100644,{blob},{tracked}") + + +@pytest.mark.parametrize( + ("tracked", "renamed_parent", "alias"), + [ + ("\u03c2.txt", None, "\u03a3.txt"), + ("stra\u00dfe.txt", None, "STRASSE.txt"), + ("caf\u00e9/x.txt", "CAF\u00c9", "CAF\u00c9/x.txt"), + ], + ids=["final-sigma", "sharp-s", "parent-renamed-non-ascii-case"], +) +def test_index_only_change_follows_file_identity( + icase_repo, tracked, renamed_parent, alias +): + repo = icase_repo + _stage(repo, tracked, b"original\n") + if renamed_parent: + # Rename the directory on disk to a spelling that differs only by non-ASCII case. + old = repo / tracked.split("/", 1)[0] + old.rename(repo / "rename-step") + (repo / "rename-step").rename(repo / renamed_parent) + if not os.path.lexists(repo / alias) or not os.path.samefile( + repo / alias, repo / tracked + ): + pytest.skip("this volume does not resolve both spellings to the same file") + before = drift_capture.stable_capture(repo, [alias]) + assert before["files"][alias]["text"] == "original\n" + _index_blob_change(repo, tracked, b"staged\n") + after = drift_capture.stable_capture(repo, [alias]) + assert after["content_digest"] == before["content_digest"] + assert after["index_digest"] != before["index_digest"] + + def test_posix_keeps_nofollow_open(repo, monkeypatch): (repo / "file.txt").write_bytes(b"original\n") platform = SimpleNamespace(**vars(os)) From fdea00cfb11014b29ef91c0a5566de058356284c Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:26:08 +0800 Subject: [PATCH 7/9] fix(jev): skip out-of-scope index paths that are not UTF-8 The full index stream decoded every record before filtering, so one unrelated non-UTF-8 path made a valid capture fail. A record that is not UTF-8 cannot be a selected path; skip it. The scoped literal read still decodes strictly, so invalid input inside the scope is still rejected. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- .../loopx-jev/src/loopx_jev/drift_capture.py | 7 +++++- .../loopx-jev/tests/test_drift_capture.py | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index 0b48f62a0d..95cd09ad16 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -292,7 +292,12 @@ def _index_records(repo: Path) -> Iterator[str]: while chunk := output.read(65536): *complete, pending = (pending + chunk).split(b"\0") for record in complete: - yield record.decode("utf-8") + try: + yield record.decode("utf-8") + except UnicodeDecodeError: + # A path that is not UTF-8 cannot be any selected path; + # it is outside the scope, so it must not fail the capture. + continue def _index(repo: Path, paths: list[str]) -> str: diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index 61c1cc8567..ec59835277 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -735,6 +735,30 @@ def test_index_only_change_follows_file_identity( assert after["index_digest"] != before["index_digest"] +def test_out_of_scope_non_utf8_index_path_does_not_fail_capture(icase_repo): + repo = icase_repo + _stage(repo, "record.txt", b"tracked\n") + blob = subprocess.run( + ["git", "-C", str(repo), "hash-object", "-w", "--stdin"], + input=b"other\n", + check=True, + capture_output=True, + ).stdout.strip() + # An index entry outside the selected scope whose path is not valid UTF-8. + added = subprocess.run( + ["git", "-C", str(repo), "update-index", "--add", "--index-info"], + input=b"100644 " + blob + b" 0\tout-of-scope-\xff.txt\n", + capture_output=True, + check=False, + ) + if added.returncode: + pytest.skip("Git refused a non-UTF-8 index path on this host") + # The missing file forces the full index stream; the tracked file reads normally. + captured = drift_capture.stable_capture(repo, ["RECORD.txt", "missing.txt"]) + assert captured["files"]["RECORD.txt"]["text"] == "tracked\n" + assert captured["files"]["missing.txt"] is None + + def test_posix_keeps_nofollow_open(repo, monkeypatch): (repo / "file.txt").write_bytes(b"original\n") platform = SimpleNamespace(**vars(os)) From c1da16e01506e6a3dc68c95a4d2844930382b76f Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:47:36 +0800 Subject: [PATCH 8/9] fix(jev): do not treat a hard link as an alias of a tracked file A selected path listed under its own spelling is a separate directory entry; sharing the file with a tracked entry does not make it that entry. Match by identity only when the selected leaf is not listed in its directory, i.e. the file system resolved it to another spelling. Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- .../loopx-jev/src/loopx_jev/drift_capture.py | 18 +++++++++++++++++- packages/loopx-jev/tests/test_drift_capture.py | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index 95cd09ad16..82a4a325e0 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -237,6 +237,13 @@ def _same_file(left: Path, right: Path) -> bool: return False +def _listed(target: Path, leaf: str) -> bool: + try: + return leaf in os.listdir(target.parent) + except OSError: + return False + + def _disk_parent(repo: Path, name: str) -> str: """Spell the existing leading directories of name as their parents list them.""" parts = name.split("/")[:-1] @@ -322,6 +329,13 @@ def _index(repo: Path, paths: list[str]) -> str: # identity, so its entry is matched by caseless name; every entry that # could be it is kept, so an index change is never reported as no_delta. existing = [name for name in pending if os.path.lexists(repo / name)] + # A selected leaf listed under its own spelling is a separate directory + # entry, so only an index entry with that exact leaf is the same entry; + # sharing a file identity (a hard link) is not an alias. An unlisted leaf + # was resolved by the file system to another spelling: match it by identity. + aliased = { + name: not _listed(repo / name, name.rsplit("/", 1)[-1]) for name in existing + } missing: dict[str, list[str]] = {} for name in pending: if name in existing: @@ -349,8 +363,10 @@ def _index(repo: Path, paths: list[str]) -> str: for name in existing if _same_file(folder, (repo / name).parent) ] + leaf = path.rsplit("/", 1)[-1] if any( - _same_file(repo / path, repo / name) + leaf == name.rsplit("/", 1)[-1] + or (aliased[name] and _same_file(repo / path, repo / name)) for name in same_directory[directory] ): records.add(record) diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index ec59835277..a78c5d53bf 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -759,6 +759,22 @@ def test_out_of_scope_non_utf8_index_path_does_not_fail_capture(icase_repo): assert captured["files"]["missing.txt"] is None +def test_hard_link_to_a_tracked_file_is_not_its_alias(icase_repo): + repo = icase_repo + _stage(repo, "record.txt", b"tracked\n") + try: + os.link(repo / "record.txt", repo / "selected.txt") + except OSError: + pytest.skip("hard links are unavailable on this volume") + # selected.txt is its own untracked directory entry that shares the file. + before = drift_capture.stable_capture(repo, ["selected.txt"]) + _index_blob_change(repo, "record.txt", b"staged\n") + after = drift_capture.stable_capture(repo, ["selected.txt"]) + assert after["content_digest"] == before["content_digest"] + # The other entry's staged change is outside the selected scope. + assert after["index_digest"] == before["index_digest"] + + def test_posix_keeps_nofollow_open(repo, monkeypatch): (repo / "file.txt").write_bytes(b"original\n") platform = SimpleNamespace(**vars(os)) From f2cb0aca7042ce44c07ad3e549f2f49cdf6c3abd Mon Sep 17 00:00:00 2001 From: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:15:19 +0800 Subject: [PATCH 9/9] fix(jev): match a case alias to its own directory entry, not a hard link A case alias of an untracked hard link shared its file identity with the tracked entry and picked up that entry's staged change. The alias is now resolved to the directory entry it opens before index entries are matched. Co-Authored-By: Claude Signed-off-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com> --- .../loopx-jev/src/loopx_jev/drift_capture.py | 30 +++++++++++++++- .../loopx-jev/tests/test_drift_capture.py | 15 ++++++++ packages/loopx-jev/uv.lock | 35 +++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 packages/loopx-jev/uv.lock diff --git a/packages/loopx-jev/src/loopx_jev/drift_capture.py b/packages/loopx-jev/src/loopx_jev/drift_capture.py index 82a4a325e0..7e5b826d28 100644 --- a/packages/loopx-jev/src/loopx_jev/drift_capture.py +++ b/packages/loopx-jev/src/loopx_jev/drift_capture.py @@ -244,6 +244,23 @@ def _listed(target: Path, leaf: str) -> bool: return False +def _entry_leaves(target: Path) -> set[str] | None: + """Spell the directory entry that target opens; None when hard links hide which one.""" + try: + names = os.listdir(target.parent) + except OSError: + return None + if target.name in names: + return {target.name} + same = [name for name in names if _same_file(target.parent / name, target)] + if len(same) > 1: + # Several entries share the file: only the caseless name tells them apart. + same = [name for name in same if _caseless(name) == _caseless(target.name)] + if len(same) != 1: + return None + return set(same) + + def _disk_parent(repo: Path, name: str) -> str: """Spell the existing leading directories of name as their parents list them.""" parts = name.split("/")[:-1] @@ -336,6 +353,10 @@ def _index(repo: Path, paths: list[str]) -> str: aliased = { name: not _listed(repo / name, name.rsplit("/", 1)[-1]) for name in existing } + # An alias names one directory entry; another entry sharing its file is not it. + entries = {name: _entry_leaves(repo / name) for name in existing if aliased[name]} + if None in entries.values(): + raise ValueError("capture_identity_unavailable") missing: dict[str, list[str]] = {} for name in pending: if name in existing: @@ -366,7 +387,14 @@ def _index(repo: Path, paths: list[str]) -> str: leaf = path.rsplit("/", 1)[-1] if any( leaf == name.rsplit("/", 1)[-1] - or (aliased[name] and _same_file(repo / path, repo / name)) + or ( + aliased[name] + and _same_file(repo / path, repo / name) + and ( + (opened := _entry_leaves(repo / path)) is None + or opened & entries[name] + ) + ) for name in same_directory[directory] ): records.add(record) diff --git a/packages/loopx-jev/tests/test_drift_capture.py b/packages/loopx-jev/tests/test_drift_capture.py index a78c5d53bf..163a8cc06f 100644 --- a/packages/loopx-jev/tests/test_drift_capture.py +++ b/packages/loopx-jev/tests/test_drift_capture.py @@ -775,6 +775,21 @@ def test_hard_link_to_a_tracked_file_is_not_its_alias(icase_repo): assert after["index_digest"] == before["index_digest"] +def test_case_alias_of_a_hard_link_ignores_the_other_entry(icase_repo): + repo = icase_repo + _stage(repo, "record.txt", b"tracked\n") + try: + os.link(repo / "record.txt", repo / "selected.txt") + except OSError: + pytest.skip("hard links are unavailable on this volume") + # SELECTED.TXT opens the untracked selected.txt entry, not the tracked record.txt. + before = drift_capture.stable_capture(repo, ["SELECTED.TXT"]) + _index_blob_change(repo, "record.txt", b"staged\n") + after = drift_capture.stable_capture(repo, ["SELECTED.TXT"]) + assert after["content_digest"] == before["content_digest"] + assert after["index_digest"] == before["index_digest"] + + def test_posix_keeps_nofollow_open(repo, monkeypatch): (repo / "file.txt").write_bytes(b"original\n") platform = SimpleNamespace(**vars(os)) diff --git a/packages/loopx-jev/uv.lock b/packages/loopx-jev/uv.lock new file mode 100644 index 0000000000..bd10a23cbe --- /dev/null +++ b/packages/loopx-jev/uv.lock @@ -0,0 +1,35 @@ +version = 1 +revision = 3 +requires-python = ">=3.11" + +[[package]] +name = "loopx" +version = "2.3.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "tzdata", marker = "sys_platform == 'win32'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/0b/a2/900ec4d0f833d340184600ae1f64bdcec3565874e3c401a33a0082c11e79/loopx-2.3.0.tar.gz", hash = "sha256:22e146f3cbc4a37bf66df1778a75ca72c4549c2b27145593742981cefa862bfe", size = 10538829, upload-time = "2026-11-09T02:36:33.931Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d5/f5/b81c35a954cccffbe9a1a5a433944462ca8ba797888bb677feeeccfbb6a3/loopx-2.3.0-py3-none-any.whl", hash = "sha256:a1ea5166058cd2275f8244356c9d17fc14ff695a2594c83e3b84b1fdc36f3caa", size = 10173135, upload-time = "2026-11-09T02:36:31.244Z" }, +] + +[[package]] +name = "loopx-jev-pilot" +version = "0.1.0" +source = { editable = "." } +dependencies = [ + { name = "loopx" }, +] + +[package.metadata] +requires-dist = [{ name = "loopx", specifier = ">=1.1.0" }] + +[[package]] +name = "tzdata" +version = "2026.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/7a/29/6a1391d31cd00ff9e927215a224a880e1e84e826b64ba80a499e51c074d8/tzdata-2026.6.tar.gz", hash = "sha256:85d2cd9c09f81f0bb29db27759fc83af4f1e620efb814f8c5bf8b4cc4feece6d", size = 201017, upload-time = "2026-10-28T09:57:09.137Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/0a/66/9a06c2227fedfd4919f0eaaa8b94377d9730882a7e4f95823c9a331ab5e1/tzdata-2026.6-py2.py3-none-any.whl", hash = "sha256:0a10eec6e13ef16b627fabed9a5f2ba61a62b1c67f327305f4e7e412a70b4aec", size = 348210, upload-time = "2026-10-28T09:57:07.276Z" }, +]