Skip to content

The agent that grows with you

fix(worktree): a retained tree's session cwd matches under Windows path identity

Teknium8267406bfb27
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index 704f461ba8f..70aa0047525 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -824,16 +824,21 @@ def _retained_tree_is_live(db_path: str, worktree_path: str) -> bool:
path = Path(db_path)
if not path.is_file():
return False
- roots = {str(Path(worktree_path)).rstrip("/\\"), str(Path(worktree_path).resolve()).rstrip("/\\")}
- inside = " OR ".join(["(s.cwd = ? OR substr(s.cwd, 1, length(?)) IN (?, ?))"] * len(roots))
- params = [v for root in roots for v in (root, root + "/", root + "/", root + "\\")]
+ def key(value) -> str: # native path identity: Windows is case-insensitive, either separator
+ return os.path.normcase(str(value)).replace("\\", "/").rstrip("/")
+
+ roots = {key(Path(worktree_path)), key(Path(worktree_path).resolve())}
+
+ def inside(cwd) -> int:
+ return int(bool(cwd) and any(key(cwd) == r or key(cwd).startswith(r + "/") for r in roots))
try:
conn = sqlite3.connect(f"file:{quote(str(path))}?mode=ro", uri=True, timeout=2)
try:
+ conn.create_function("hermes_inside_tree", 1, inside, deterministic=True)
row = conn.execute(
- f"SELECT 1 FROM sessions s WHERE ({inside}) AND (s.message_count>0 OR EXISTS ("
+ "SELECT 1 FROM sessions s WHERE hermes_inside_tree(s.cwd) AND (s.message_count>0 OR EXISTS ("
"SELECT 1 FROM session_admissions a WHERE a.target_session_id=s.id AND a.status!='terminal')) "
- "LIMIT 1", params).fetchone()
+ "LIMIT 1").fetchone()
finally:
conn.close()
except sqlite3.Error as exc:
diff --git a/tests/hermes_cli/test_worktree_retained_cwd_identity.py b/tests/hermes_cli/test_worktree_retained_cwd_identity.py
new file mode 100644
index 00000000000..cab2b79f641
--- /dev/null
+++ b/tests/hermes_cli/test_worktree_retained_cwd_identity.py
@@ -0,0 +1,34 @@
+"""A retained ``--tui -w`` tree stays live when its session's stored cwd spells the same native
+path differently (Windows: case-insensitive, either separator)."""
+import ntpath
+import os
+import sqlite3
+
+import pytest
+
+from hermes_cli.worktree_ops import _retained_tree_is_live
+
+
+def _store(tmp_path, cwd):
+ db = tmp_path / 'state.db'
+ conn = sqlite3.connect(db)
+ conn.executescript('CREATE TABLE sessions (id TEXT, cwd TEXT, message_count INTEGER);'
+ 'CREATE TABLE session_admissions (target_session_id TEXT, status TEXT);')
+ conn.execute('INSERT INTO sessions VALUES (?, ?, 3)', ('s1', cwd))
+ conn.commit()
+ conn.close()
+ return str(db)
+
+
[email protected]('spell, live', [
+ (lambda p: p.upper(), True), # same path, other casing
+ (lambda p: p.upper().replace('/', '\\') + '\\sub', True), # nested, backslash separators
+ (lambda p: p + '-other', False), # sibling prefix is not inside
+ (lambda p: os.path.dirname(p), False), # parent dir is not inside
+])
+def test_retained_tree_uses_windows_path_identity(tmp_path, monkeypatch, spell, live):
+ tree = tmp_path / 'Tree'
+ tree.mkdir()
+ db = _store(tmp_path, spell(str(tree.resolve())))
+ monkeypatch.setattr(os.path, 'normcase', ntpath.normcase) # the native Windows identity
+ assert _retained_tree_is_live(db, str(tree)) is live