Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(learn): handle Windows UTF-8, drive-letter paths, and CLI shim fallback (#1895)

Rod Boeve3b45e402bc0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dba81821d..718cd844c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,26 +8,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
-### Fixed
-- The Anthropic Messages route (`POST /v1/messages`) now honors the
- `x-headroom-base-url` per-request upstream override. It previously ignored the
- header and always forwarded to `api.anthropic.com`, so clients that speak the
- Anthropic Messages wire format while authenticating against a non-Anthropic
- gateway (e.g. OpenCode Zen) were rejected upstream with `401 invalid
- x-api-key`. The route now forwards to `<x-headroom-base-url>/v1/messages`,
- consistent with the OpenAI-compatible and passthrough routes
- ([#1760](https://github.com/headroomlabs-ai/headroom/issues/1760)).
-- **proxy:** the savings store now fsyncs its parent directory after the
- atomic rename, so the most recent `proxy_savings.json` write survives a
- power-loss or crash. `_save_locked` fsynced the temp file's contents but
- never the directory entry the rename created, leaving the rename itself
- non-durable on POSIX. Best-effort — a no-op on Windows and virtual
- filesystems where directory fsync is unsupported.
-
-### Features
-
-* **transforms:** add opt-in audit-safe mode to `SmartCrusher` — `SmartCrusherConfig(audit_safe=True, protected_patterns=[...], fail_closed_on_protected_loss=True)`. Rows matching a protected pattern are scanned before JSON-array compression and guaranteed to survive the compressed output verbatim afterward (never dropped, never replaced by an opaque `<<ccr:...>>` marker only). Applies on both the `crush_array_json` convenience API and the `_smart_crush_content` path `apply()` uses for real tool-output compression. If a protected row still can't be preserved after the splice-back pass, the crusher fails closed by returning the original uncompressed content (or ships a best-effort result with a warning when `fail_closed_on_protected_loss=False`). Default is `audit_safe=False` — no behavior change for existing callers ([#1705](https://github.com/chopratejas/headroom/issues/1705)).
-
### Changed
* **telemetry:** anonymous usage telemetry is now **opt-in** (off by default) instead of opt-out. Nothing is collected or sent unless you set `HEADROOM_TELEMETRY=on` or pass `--telemetry` to `headroom proxy` / `headroom install apply`. `is_telemetry_enabled()` is fail-closed — only explicit on-values (`on`/`true`/`1`/`yes`/`enable`/`enabled`) enable it; unset, empty, or unrecognized values stay disabled. The existing `--no-telemetry` flag and `HEADROOM_TELEMETRY=off` remain accepted for back-compat, and install manifests now write the `HEADROOM_TELEMETRY` value explicitly so generated deployments are unambiguous.
@@ -48,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* **proxy:** cross-region Bedrock inference-profile detection — geo-prefixed model IDs (`eu.`/`us.`/`apac.`/`global.`) are now resolved to their canonical vendor, so Anthropic cross-region profiles (e.g. `eu.anthropic.claude-haiku-4-5-20251001-v1:0`) receive live-zone compression instead of being silently skipped ([#999](https://github.com/chopratejas/headroom/pull/999)).
* **proxy:** Converse-body compression on the native Bedrock route — the live-zone dispatcher now recognizes Bedrock Converse content blocks (typeless `{"text": …}`, not only Anthropic `{"type":"text", …}`), so Converse user-message text compresses; `run_anthropic_compression` no longer bails to passthrough when the body lacks an InvokeModel `anthropic_version` envelope, and envelope re-emit stays gated on successful parse ([#999](https://github.com/chopratejas/headroom/pull/999)).
* **docker:** bundle `headroom-proxy` binary in published `runtime` and `runtime-slim` images — closes [#976](https://github.com/chopratejas/headroom/issues/976) ([#999](https://github.com/chopratejas/headroom/pull/999)).
+* **transforms:** add opt-in audit-safe mode to `SmartCrusher` — `SmartCrusherConfig(audit_safe=True, protected_patterns=[...], fail_closed_on_protected_loss=True)`. Rows matching a protected pattern are scanned before JSON-array compression and guaranteed to survive the compressed output verbatim afterward (never dropped, never replaced by an opaque `<<ccr:...>>` marker only). Applies on both the `crush_array_json` convenience API and the `_smart_crush_content` path `apply()` uses for real tool-output compression. If a protected row still can't be preserved after the splice-back pass, the crusher fails closed by returning the original uncompressed content (or ships a best-effort result with a warning when `fail_closed_on_protected_loss=False`). Default is `audit_safe=False` — no behavior change for existing callers ([#1705](https://github.com/chopratejas/headroom/issues/1705)).
### Bug Fixes
@@ -121,6 +102,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* **ccr:** buffered Anthropic CCR re-streaming now preserves adaptive-thinking response shape, including empty `thinking` blocks, `signature_delta`, `redacted_thinking.data`, verbatim `stop_reason` values such as `refusal`, and `stop_details`.
* **cursor:** `headroom wrap cursor` no longer injects the `rtk` custom-instructions block into `.cursorrules` when rtk's own native Cursor hook registers successfully. rtk supports a real hook for Cursor via `rtk init --agent cursor` (the same mechanism headroom already uses for Claude Code), which rewrites shell commands transparently — the injected `.cursorrules` text duplicated that guidance for no benefit. `wrap cursor` now tries the native hook first and only falls back to injecting `.cursorrules` if hook registration fails (#756).
* **proxy:** The Headroom dashboard no longer tunnels `GET /favicon.ico` to the wrapped upstream provider. No route matched that path, so it fell through to the proxy's catch-all passthrough route and was forwarded to the configured Anthropic/OpenAI/etc. backend — burning a real upstream request (and possibly failing auth) for a browser's automatic favicon fetch on `/dashboard`. A dedicated `/favicon.ico` route now answers with `204 No Content` directly, registered ahead of the passthrough catch-all (#1787).
+* **learn:** fix three Windows-specific failures in `headroom learn --verbosity` and CLI-backed analysis ([#1624](https://github.com/headroomlabs-ai/headroom/issues/1624)). `verbosity.py` read transcripts and profiles with the platform-default text codec instead of UTF-8, so non-ASCII content raised a silently-caught `UnicodeDecodeError`, producing `Sessions: 0, human turns: 0` for every project. `_greedy_path_decode` listed a directory's children with `is_dir()` inline in the same expression as `iterdir()`, so a single `PermissionError` on an inaccessible sibling (e.g. the `AppData\Local\Temporary Internet Files` junction present on most Windows profiles) aborted the whole listing and silently mis-decoded any project path that walked through it, causing `--project <path>` to report "No matching project" or resolve the wrong directory. `_call_cli_llm` launched CLI backends via `Popen`/`run`, which use `CreateProcess` on Windows and don't apply the shell's `PATHEXT` extension search, so an npm-installed `.cmd` shim (e.g. `claude`, `codex`) raised `FileNotFoundError` even though it was on `PATH`; a `shutil.which`-based retry now resolves the shim.
+* **proxy:** The Anthropic Messages route (`POST /v1/messages`) now honors the `x-headroom-base-url` per-request upstream override. It previously ignored the header and always forwarded to `api.anthropic.com`, so clients that speak the Anthropic Messages wire format while authenticating against a non-Anthropic gateway (e.g. OpenCode Zen) were rejected upstream with `401 invalid x-api-key`. The route now forwards to `<x-headroom-base-url>/v1/messages`, consistent with the OpenAI-compatible and passthrough routes ([#1760](https://github.com/headroomlabs-ai/headroom/issues/1760)).
+* **proxy:** the savings store now fsyncs its parent directory after the atomic rename, so the most recent `proxy_savings.json` write survives a power-loss or crash. `_save_locked` fsynced the temp file's contents but never the directory entry the rename created, leaving the rename itself non-durable on POSIX. Best-effort — a no-op on Windows and virtual filesystems where directory fsync is unsupported.
## [0.31.0](https://github.com/headroomlabs-ai/headroom/compare/v0.30.0...v0.31.0) (2026-07-09)
diff --git a/headroom/learn/analyzer.py b/headroom/learn/analyzer.py
index b786f8c1b..7c6b12277 100644
--- a/headroom/learn/analyzer.py
+++ b/headroom/learn/analyzer.py
@@ -73,6 +73,24 @@ _CLI_TIMEOUT = 300
_CLI_IDLE_TIMEOUT = 60
+def _resolve_windows_cli_shim(cmd: list[str]) -> list[str] | None:
+ """Resolve an npm-installed CLI shim to its real executable on Windows.
+
+ ``subprocess`` launches via ``CreateProcess`` on Windows, which — unlike a
+ shell — does not apply the ``PATHEXT`` extension search. An npm-installed
+ CLI's PATH entry is usually a ``.cmd``/``.bat`` shim, so the bare command
+ name raises ``FileNotFoundError`` even though ``shutil.which`` (which does
+ apply ``PATHEXT``) resolves it fine. Re-resolve through ``shutil.which``
+ and retry with the resolved path.
+ """
+ if os.name != "nt":
+ return None
+ resolved = shutil.which(cmd[0])
+ if resolved is None:
+ return None
+ return [resolved, *cmd[1:]]
+
+
def _resolve_timeout_secs(env_var: str, default: int) -> int:
"""Resolve a positive-integer timeout from *env_var* or fall back to *default*.
@@ -528,10 +546,20 @@ def _call_cli_llm(digest: str, model: str) -> dict:
timeout=hard_cap,
)
except FileNotFoundError:
- raise RuntimeError(
- f"`{cmd[0]}` not found in PATH. Install it or use a different backend "
- "with --model <litellm-model-name>."
- ) from None
+ shim_cmd = _resolve_windows_cli_shim(cmd)
+ if shim_cmd is None:
+ raise RuntimeError(
+ f"`{cmd[0]}` not found in PATH. Install it or use a different backend "
+ "with --model <litellm-model-name>."
+ ) from None
+ cmd = shim_cmd
+ try:
+ result = run(cmd, input=prompt, capture_output=True, text=True, timeout=hard_cap)
+ except FileNotFoundError:
+ raise RuntimeError(
+ f"`{cmd[0]}` not found in PATH. Install it or use a different backend "
+ "with --model <litellm-model-name>."
+ ) from None
except subprocess.TimeoutExpired:
raise RuntimeError(
f"`{' '.join(cmd)}` did not respond within {hard_cap}s. "
@@ -573,8 +601,9 @@ def _call_claude_cli_streaming(
Threads (rather than ``select``) drain stdout/stderr so the watchdog works
on Windows too, where ``select`` does not support pipe handles.
"""
- try:
- proc = Popen(
+
+ def _popen(cmd: list[str]) -> subprocess.Popen:
+ return Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
@@ -582,11 +611,24 @@ def _call_claude_cli_streaming(
text=True,
bufsize=1, # line-buffered
)
+
+ try:
+ proc = _popen(cmd)
except FileNotFoundError:
- raise RuntimeError(
- f"`{cmd[0]}` not found in PATH. Install it or use a different backend "
- "with --model <litellm-model-name>."
- ) from None
+ shim_cmd = _resolve_windows_cli_shim(cmd)
+ if shim_cmd is None:
+ raise RuntimeError(
+ f"`{cmd[0]}` not found in PATH. Install it or use a different backend "
+ "with --model <litellm-model-name>."
+ ) from None
+ cmd = shim_cmd
+ try:
+ proc = _popen(cmd)
+ except FileNotFoundError:
+ raise RuntimeError(
+ f"`{cmd[0]}` not found in PATH. Install it or use a different backend "
+ "with --model <litellm-model-name>."
+ ) from None
assert proc.stdin is not None and proc.stdout is not None and proc.stderr is not None
try:
diff --git a/headroom/learn/plugins/claude.py b/headroom/learn/plugins/claude.py
index 79d1b331d..185ba32de 100644
--- a/headroom/learn/plugins/claude.py
+++ b/headroom/learn/plugins/claude.py
@@ -96,6 +96,11 @@ class ClaudeCodePlugin(LearnPlugin, ConversationScanner):
if not jsonl_files:
continue
+ session_project_path = self._project_path_from_session_cwd(jsonl_files)
+ if session_project_path is not None:
+ project_path = session_project_path
+ name = _project_display_name(project_path, entry.name)
+
projects.append(
ProjectInfo(
name=name,
@@ -108,6 +113,27 @@ class ClaudeCodePlugin(LearnPlugin, ConversationScanner):
return projects
+ @staticmethod
+ def _project_path_from_session_cwd(jsonl_files: list[Path]) -> Path | None:
+ for jsonl_path in sorted(jsonl_files):
+ try:
+ with open(jsonl_path, encoding="utf-8", errors="replace") as f:
+ for line in f:
+ if not line.strip():
+ continue
+ try:
+ event = json.loads(line)
+ except json.JSONDecodeError:
+ continue
+ cwd = event.get("cwd")
+ if isinstance(cwd, str) and cwd:
+ project_path = Path(cwd)
+ if project_path.exists():
+ return project_path
+ except (OSError, UnicodeDecodeError):
+ continue
+ return None
+
def scan_project(
self, project: ProjectInfo, max_workers: int = 1, include_subagents: bool = True
) -> list[SessionData]:
@@ -420,10 +446,24 @@ def _greedy_path_decode(base: Path, parts: list[str]) -> Path | None:
return None
try:
- children = sorted(child for child in base.iterdir() if child.is_dir())
+ entries = list(base.iterdir())
except OSError:
return None
+ # Windows profiles routinely contain reparse-point junctions (e.g.
+ # "AppData\Local\Temporary Internet Files") that raise PermissionError on
+ # is_dir(). Skip those entries individually instead of letting one
+ # inaccessible sibling abort the whole listing — and thus every project
+ # path that happens to walk through this directory.
+ children = []
+ for entry in entries:
+ try:
+ if entry.is_dir():
+ children.append(entry)
+ except OSError:
+ continue
+ children.sort()
+
for child in children:
for tokenization in _component_tokenizations(child.name):
n_tokens = len(tokenization)
diff --git a/headroom/learn/verbosity.py b/headroom/learn/verbosity.py
index 5b6ccb664..4d428c922 100644
--- a/headroom/learn/verbosity.py
+++ b/headroom/learn/verbosity.py
@@ -144,7 +144,7 @@ class VerbosityProfile:
@classmethod
def load(cls, path: Path) -> VerbosityProfile | None:
try:
- d = json.loads(Path(path).read_text())
+ d = json.loads(Path(path).read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError, ValueError):
return None
return cls(
@@ -159,7 +159,7 @@ class VerbosityProfile:
def save(self, path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
- path.write_text(json.dumps(self.to_dict(), indent=2))
+ path.write_text(json.dumps(self.to_dict(), indent=2), encoding="utf-8")
def _parse_ts(s: str | None) -> float | None:
@@ -208,7 +208,7 @@ def _parse_session(path: Path) -> tuple[list[_Response], list[_HumanMsg], bool]:
recent_context: list[str] = []
try:
- lines = path.read_text().splitlines()
+ lines = path.read_text(encoding="utf-8").splitlines()
except (OSError, UnicodeDecodeError):
return [], [], False
@@ -282,7 +282,7 @@ def _ordered_events(path: Path) -> list[tuple[float | None, str, _Response | _Hu
"""
out: list[tuple[float | None, str, Any]] = []
try:
- lines = path.read_text().splitlines()
+ lines = path.read_text(encoding="utf-8").splitlines()
except (OSError, UnicodeDecodeError):
return out
responses, humans, _ = _parse_session(path)
diff --git a/tests/test_learn/test_analyzer.py b/tests/test_learn/test_analyzer.py
index 1e4c18208..fa88b15b9 100644
--- a/tests/test_learn/test_analyzer.py
+++ b/tests/test_learn/test_analyzer.py
@@ -824,6 +824,87 @@ class TestCallCliLlm:
_call_cli_llm("test digest", "codex-cli")
+class TestWindowsCliShimFallback:
+ """npm-installed CLIs on Windows are ``.cmd``/``.bat`` shims, not directly
+ executable — ``subprocess`` uses ``CreateProcess``, which (unlike a shell)
+ does not apply the ``PATHEXT`` extension search, so ``Popen`` raises
+ ``FileNotFoundError`` even though the shell (and ``shutil.which``) finds
+ the CLI fine (issue #1624). A FileNotFoundError on Windows should trigger
+ one ``shutil.which``-based retry with the resolved executable path.
+ """
+
+ def test_streaming_cli_retries_with_resolved_shim(self, monkeypatch):
+ monkeypatch.setattr("headroom.learn.analyzer.os.name", "nt")
+ monkeypatch.setattr(
+ "headroom.learn.analyzer.shutil.which",
+ lambda name: r"C:\npm\claude.cmd" if name == "claude" else None,
+ )
+ stdout = [_result_event('{"context_file_rules": [], "memory_file_rules": []}')]
+ fake_popen = _fake_claude_popen(stdout_lines=stdout)
+ calls: list[list[str]] = []
+
+ def _construct(cmd, *args, **kwargs):
+ calls.append(cmd)
+ if cmd[0] == "claude":
+ raise FileNotFoundError("No such file or directory: 'claude'")
+ return fake_popen.side_effect(cmd, *args, **kwargs)
+
+ popen = MagicMock(side_effect=_construct)
+ with patch("headroom.learn.analyzer.subprocess.Popen", popen):
+ result = _call_cli_llm("test digest", "claude-cli")
+
+ assert result == {"context_file_rules": [], "memory_file_rules": []}
+ assert calls[0][0] == "claude"
+ assert calls[1][0] == r"C:\npm\claude.cmd"
+ assert calls[1][1:] == calls[0][1:] # remaining args preserved
+
+ def test_non_streaming_cli_retries_with_resolved_shim(self, monkeypatch):
+ monkeypatch.setattr("headroom.learn.analyzer.os.name", "nt")
+ monkeypatch.setattr(
+ "headroom.learn.analyzer.shutil.which",
+ lambda name: r"C:\npm\codex.cmd" if name == "codex" else None,
+ )
+ calls: list[list[str]] = []
+
+ def _run(cmd, *args, **kwargs):
+ calls.append(cmd)
+ if cmd[0] == "codex":
+ raise FileNotFoundError("No such file or directory: 'codex'")
+ return MagicMock(
+ returncode=0,
+ stdout='{"context_file_rules": [], "memory_file_rules": []}',
+ stderr="",
+ )
+
+ with patch("headroom.learn.analyzer.subprocess.run", side_effect=_run):
+ result = _call_cli_llm("test digest", "codex-cli")
+
+ assert result == {"context_file_rules": [], "memory_file_rules": []}
+ assert calls[0][0] == "codex"
+ assert calls[1][0] == r"C:\npm\codex.cmd"
+
+ def test_shim_unresolvable_still_raises_not_found_in_path(self, monkeypatch):
+ monkeypatch.setattr("headroom.learn.analyzer.os.name", "nt")
+ monkeypatch.setattr("headroom.learn.analyzer.shutil.which", lambda name: None)
+ mock_run = MagicMock(side_effect=FileNotFoundError("No such file or directory: 'codex'"))
+ with patch("headroom.learn.analyzer.subprocess.run", mock_run):
+ with pytest.raises(RuntimeError, match="not found in PATH"):
+ _call_cli_llm("test digest", "codex-cli")
+ # shutil.which couldn't resolve anything — no retry attempted.
+ assert mock_run.call_count == 1
+
+ def test_non_windows_skips_shim_resolution(self, monkeypatch):
+ monkeypatch.setattr("headroom.learn.analyzer.os.name", "posix")
+ which = MagicMock(return_value=r"/usr/local/bin/codex")
+ monkeypatch.setattr("headroom.learn.analyzer.shutil.which", which)
+ mock_run = MagicMock(side_effect=FileNotFoundError("No such file or directory: 'codex'"))
+ with patch("headroom.learn.analyzer.subprocess.run", mock_run):
+ with pytest.raises(RuntimeError, match="not found in PATH"):
+ _call_cli_llm("test digest", "codex-cli")
+ which.assert_not_called()
+ assert mock_run.call_count == 1
+
+
class TestParseStreamEvent:
def test_returns_none_for_empty_line(self):
from headroom.learn.analyzer import _parse_stream_event
diff --git a/tests/test_learn/test_scanner.py b/tests/test_learn/test_scanner.py
index 2e5aafd04..15b7aa718 100644
--- a/tests/test_learn/test_scanner.py
+++ b/tests/test_learn/test_scanner.py
@@ -8,6 +8,7 @@ making it impossible to reconstruct names formed from three or more tokens.
from __future__ import annotations
+import json
from collections.abc import Generator
from pathlib import Path
from uuid import uuid4
@@ -160,6 +161,32 @@ class TestGreedyPathDecode:
result = _greedy_path_decode(tmp_path, ["does", "not", "exist"])
assert result is None
+ def test_permission_denied_sibling_does_not_abort_the_walk(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ """A single inaccessible sibling must not hide every other match (#1624).
+
+ Real Windows profiles routinely contain reparse-point junctions (e.g.
+ ``AppData\\Local\\Temporary Internet Files``) that raise
+ ``PermissionError`` on ``is_dir()``. The old code listed
+ ``sorted(child for child in base.iterdir() if child.is_dir())`` in one
+ expression, so a single inaccessible sibling raised OSError out of the
+ whole comprehension and the entire directory's children — including the
+ one actually being decoded — were silently discarded, returning None.
+ """
+ _make_dirs(tmp_path, "Blocked", "real-target")
+ original_is_dir = Path.is_dir
+
+ def _guarded_is_dir(self: Path) -> bool:
+ if self.name == "Blocked":
+ raise PermissionError("Access is denied")
+ return original_is_dir(self)
+
+ monkeypatch.setattr(Path, "is_dir", _guarded_is_dir)
+
+ result = _greedy_path_decode(tmp_path, ["real", "target"])
+ assert result == tmp_path / "real-target"
+
def test_empty_parts_returns_base_when_exists(self, tmp_path: Path) -> None:
result = _greedy_path_decode(tmp_path, [])
assert result == tmp_path
@@ -391,6 +418,25 @@ class TestDecodeProjectPath:
assert projects[0].name == "work"
assert str(projects[0].project_path).startswith("C:")
+ def test_discover_project_prefers_session_cwd_over_ambiguous_folder_name(
+ self, tmp_path: Path
+ ) -> None:
+ nested = tmp_path / "vibe" / "remote"
+ hyphenated = tmp_path / "vibe-remote"
+ nested.mkdir(parents=True)
+ hyphenated.mkdir()
+
+ claude_dir = tmp_path / ".claude"
+ project_dir = claude_dir / "projects" / "C--Users-rod-work-vibe-remote"
+ project_dir.mkdir(parents=True)
+ (project_dir / "session.jsonl").write_text(json.dumps({"cwd": str(hyphenated)}) + "\n")
+
+ projects = ClaudeCodeScanner(claude_dir=claude_dir).discover_projects()
+
+ assert len(projects) == 1
+ assert projects[0].name == "vibe-remote"
+ assert projects[0].project_path == hyphenated
+
def test_windows_double_dash_encoding_decodes(self) -> None:
"""Real Claude Code encoding has no leading dash: C:\\Users\\x → C--Users-x (#1849).
@@ -425,6 +471,49 @@ class TestDecodeProjectPath:
result = _decode_project_path(encoded)
assert result == project
+ def test_windows_hyphenated_leaf_under_permission_denied_ancestor(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ """D:\\work\\vibe-remote must decode correctly even when an ancestor
+ directory has an inaccessible sibling (#1624).
+
+ ``headroom learn --verbosity --project`` reported "No matching
+ project" on real Windows machines: the naive full-token join
+ (``vibe-remote`` split into ``vibe`` + ``remote``) doesn't exist, so
+ decoding falls through to the greedy walk — which real Windows user
+ profiles abort early on an inaccessible junction such as
+ ``AppData\\Local\\Temporary Internet Files``, long before reaching the
+ project directory itself.
+ """
+ import sys
+ import tempfile
+
+ if sys.platform != "win32":
+ pytest.skip("greedy Windows-path decode requires real Windows filesystem")
+
+ with tempfile.TemporaryDirectory() as td:
+ root = Path(td)
+ (root / "Blocked").mkdir()
+ project = root / "work" / "vibe-remote"
+ project.mkdir(parents=True)
+
+ original_is_dir = Path.is_dir
+
+ def _guarded_is_dir(self: Path) -> bool:
+ if self.name == "Blocked":
+ raise PermissionError("Access is denied")
+ return original_is_dir(self)
+
+ monkeypatch.setattr(Path, "is_dir", _guarded_is_dir)
+
+ drive = root.drive[0]
+ rest = str(root)[3:] # strip 'C:\\'
+ rest_parts = rest.replace("\\", "-") if rest else ""
+ encoded = f"{drive}--" + "-".join(p for p in (rest_parts, "work-vibe-remote") if p)
+
+ result = _decode_project_path(encoded)
+ assert result == project
+
def test_discover_double_dash_windows_project_fallback(self, tmp_path: Path) -> None:
"""Nonexistent C--Users-... project must fall back to a valid path, not \\\\\\Users (#1849)."""
claude_dir = tmp_path / ".claude"
diff --git a/tests/test_verbosity_learn.py b/tests/test_verbosity_learn.py
index 5df6b64cb..538a3569f 100644
--- a/tests/test_verbosity_learn.py
+++ b/tests/test_verbosity_learn.py
@@ -6,7 +6,9 @@ import json
from pathlib import Path
from headroom.learn.verbosity import (
+ VerbosityProfile,
VerbositySignals,
+ _parse_session,
analyze,
extract_signals,
recommend_level,
@@ -249,3 +251,70 @@ class TestAnalyze:
from headroom.learn.verbosity import VerbosityProfile
assert VerbosityProfile.load(tmp_path / "nope.json") is None
+
+
+class TestWindowsEncoding:
+ """Windows defaults text I/O without an explicit encoding to a locale
+ codec (e.g. GBK, cp1252) rather than UTF-8. Transcripts containing
+ non-ASCII text then raised UnicodeDecodeError, which was silently
+ swallowed and produced "Sessions: 0, human turns: 0" (issue #1624).
+
+ ``locale.getpreferredencoding`` is monkeypatched to simulate that
+ non-UTF-8 default on any platform, including the UTF-8-default CI/dev
+ machines this suite normally runs on.
+ """
+
+ def _write_utf8_session(self, tmp_path: Path, name: str, lines: list[dict]) -> Path:
+ p = tmp_path / f"{name}.jsonl"
+ p.write_text(
+ "\n".join(json.dumps(line, ensure_ascii=False) for line in lines),
+ encoding="utf-8",
+ )
+ return p
+
+ def test_parse_session_reads_non_ascii_under_non_utf8_locale(self, tmp_path, monkeypatch):
+ monkeypatch.setattr("locale.getpreferredencoding", lambda do_setlocale=True: "cp1252")
+ p = self._write_utf8_session(
+ tmp_path,
+ "s",
+ [
+ _user("你好,请帮我写代码", ts="2026-01-01T00:00:00Z"),
+ _assistant("好的," + LONG, ts="2026-01-01T00:00:01Z"),
+ ],
+ )
+ responses, humans, _ = _parse_session(p)
+ assert len(responses) == 1
+ assert len(humans) == 1
+
+ def test_extract_signals_not_empty_under_non_utf8_locale(self, tmp_path, monkeypatch):
+ monkeypatch.setattr("locale.getpreferredencoding", lambda do_setlocale=True: "cp1252")
+ p = self._write_utf8_session(
+ tmp_path,
+ "s",
+ [
+ _user("開始してください", ts="2026-01-01T00:00:00Z"),
+ _assistant(LONG, ts="2026-01-01T00:00:01Z"),
+ ],
+ )
+ sig, _ = extract_signals([p])
+ assert sig.sessions == 1
+ assert sig.asst_responses == 1
+
+ def test_profile_save_load_roundtrip_non_ascii_under_non_utf8_locale(
+ self, tmp_path, monkeypatch
+ ):
+ monkeypatch.setattr("locale.getpreferredencoding", lambda do_setlocale=True: "cp1252")
+ prof = VerbosityProfile(
+ project_path="D:\\work\\DPJ",
+ level=2,
+ confidence="high",
+ source="heuristic",
+ rationale="用户回复很快,倾向于更简短的回答",
+ signals={},
+ )
+ path = tmp_path / "verbosity.json"
+ prof.save(path)
+ loaded = VerbosityProfile.load(path)
+ assert loaded is not None
+ assert loaded.rationale == prof.rationale
+ assert loaded.project_path == prof.project_path