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): decode Unix home dirs whose username contains '.', '-' or '_'

MrAshRhodes491a8b3a1b26
diff --git a/headroom/learn/plugins/claude.py b/headroom/learn/plugins/claude.py
index 6d5a20025..2be4f9e7e 100644
--- a/headroom/learn/plugins/claude.py
+++ b/headroom/learn/plugins/claude.py
@@ -333,15 +333,17 @@ def _decode_project_path(escaped_name: str) -> Path | None:
if len(parts) < 3:
return None
- if parts[0] == "Users" and len(parts) > 2:
+ if parts[0] in ("Users", "home") and len(parts) > 2:
+ # Start the greedy decode at the mount root so a home-directory
+ # component containing '.', '-' or '_' (e.g. "first.last", encoded as
+ # "first-last") is matched as a single directory by tokenisation,
+ # instead of being split into "/Users/first/last". Falls back to the
+ # legacy single-token assumption if the rooted walk finds nothing.
+ result = _greedy_path_decode(Path(f"/{parts[0]}"), parts[1:])
+ if result is not None:
+ return result
base = Path(f"/{parts[0]}/{parts[1]}")
- remaining = parts[2:]
- return _greedy_path_decode(base, remaining)
-
- if parts[0] == "home" and len(parts) > 2:
- base = Path(f"/{parts[0]}/{parts[1]}")
- remaining = parts[2:]
- return _greedy_path_decode(base, remaining)
+ return _greedy_path_decode(base, parts[2:])
return None
diff --git a/tests/test_learn/test_scanner.py b/tests/test_learn/test_scanner.py
index e35cd79ed..f59c61fed 100644
--- a/tests/test_learn/test_scanner.py
+++ b/tests/test_learn/test_scanner.py
@@ -344,3 +344,37 @@ class TestDecodeProjectPath:
assert len(projects) == 1
assert projects[0].name == "work"
assert str(projects[0].project_path).startswith("C:")
+
+ def test_unix_username_with_dot_stays_single_component(
+ self, users_tmp: Path
+ ) -> None:
+ """Unix home dirs like /Users/first.last must not decode as /Users/first/last.
+
+ Claude Code flattens '.', '-' and '_' to '-' when escaping, so
+ /Users/first.last is stored as ``-Users-first-last-…``. The decoder used
+ to consume only the first token after ``Users`` as the home directory and
+ walk from ``/Users/first`` (which does not exist), so it bailed out and
+ callers fell back to the literal ``/Users/first/last`` — causing writes to
+ fail with ``PermissionError: '/Users/first'``. This is the Unix
+ counterpart of ``test_windows_username_with_dot_stays_single_component``.
+
+ Reproduces only when the test runner's home is itself a dotted/hyphenated
+ name under ``/Users`` (the decoder is rooted at the real ``/Users`` tree);
+ skipped otherwise.
+ """
+ import re
+
+ home = Path.home()
+ if not (str(home).startswith("/Users/") and ("." in home.name or "-" in home.name)):
+ pytest.skip("requires a dotted or hyphenated username under /Users")
+
+ project = users_tmp / "proj"
+ project.mkdir(parents=True)
+
+ # Flatten separators exactly as Claude Code does when escaping a path.
+ encoded = "-" + re.sub(r"[-._/]", "-", str(project)[1:])
+ result = _decode_project_path(encoded)
+
+ assert result == project
+ assert f"/{home.name}/" in str(result)
+ assert f"/{home.name.split('.')[0]}/" not in str(result)