Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion hermes_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,16 @@ def _delegate_from_json(col: str = "model_config") -> str:

def _cwd_prefix_clause(cwd_prefix: str) -> Tuple[str, List[str]]:
prefix = cwd_prefix.rstrip("/\\") or cwd_prefix
return "(s.cwd = ? OR s.cwd LIKE ? OR s.cwd LIKE ?)", [prefix, f"{prefix}/%", f"{prefix}\\%"]
# ``_`` and ``%`` are LIKE wildcards but ordinary characters in a path
# (``my_project``), so an unescaped prefix also matches sibling directories.
# Escape the needle and pair it with ESCAPE; the literal separator
# backslash in the Windows pattern needs escaping for the same reason. The
# ``=`` arm is an exact compare and keeps the raw prefix.
esc = prefix.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
return (
"(s.cwd = ? OR s.cwd LIKE ? ESCAPE '\\' OR s.cwd LIKE ? ESCAPE '\\')",
[prefix, f"{esc}/%", f"{esc}\\\\%"],
)


def _workspace_key_clause(key: str) -> Tuple[str, List[str]]:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_hermes_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,35 @@ def _mk_rich(db, sid, **cols):



def test_cwd_prefix_underscore_is_literal_not_a_wildcard(self, db):
"""``_`` is a LIKE wildcard but an ordinary character in a path, so an
unescaped prefix also matched a same-length sibling directory — and
prune_sessions deletes what it matches."""
self._mk(db, "target", cwd="/home/me/my_project/src")
self._mk(db, "sibling", cwd="/home/me/myXproject/src")

rows = db.list_prune_candidates(cwd_prefix="/home/me/my_project")
assert {r["id"] for r in rows} == {"target"}

pruned = db.prune_sessions(older_than_days=None, cwd_prefix="/home/me/my_project")
assert pruned == 1
assert db.get_session("sibling") is not None

def test_cwd_prefix_percent_does_not_select_everything(self, db):
self._mk(db, "a", cwd="/home/me/one")
self._mk(db, "b", cwd="/home/me/two")

assert db.list_prune_candidates(cwd_prefix="/home/me/%") == []

def test_cwd_prefix_still_matches_the_directory_and_its_children(self, db):
"""Control: the prefix must keep matching itself and anything under it."""
self._mk(db, "root", cwd="/home/me/proj")
self._mk(db, "child", cwd="/home/me/proj/src")
self._mk(db, "outside", cwd="/home/me/other")

rows = db.list_prune_candidates(cwd_prefix="/home/me/proj")
assert {r["id"] for r in rows} == {"root", "child"}

def test_unknown_filter_rejected(self, db):
import pytest as _pytest
with _pytest.raises(TypeError):
Expand Down
Loading