Skip to content

fix(sessions): escape LIKE wildcards in the cwd-prefix clause - #78927

Closed
Drexuxux wants to merge 1 commit into
NousResearch:mainfrom
Drexuxux:fix/prune-cwd-prefix-like-wildcards
Closed

fix(sessions): escape LIKE wildcards in the cwd-prefix clause#78927
Drexuxux wants to merge 1 commit into
NousResearch:mainfrom
Drexuxux:fix/prune-cwd-prefix-like-wildcards

Conversation

@Drexuxux

@Drexuxux Drexuxux commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

What

_cwd_prefix_clause builds the "cwd is this directory or somewhere under it" predicate used by session listing, workspace resume, and prune/archive. Both LIKE arms bound the raw prefix:

return "(s.cwd = ? OR s.cwd LIKE ? OR s.cwd LIKE ?)", [prefix, f"{prefix}/%", f"{prefix}\\%"]

_ and % are LIKE wildcards, and both are ordinary characters in a path. _ matches any single character, so a same-length sibling directory with children falls inside the pattern. Real SessionDB, real SQLite:

cwd_prefix="/home/me/my_project"
  main -> ['sibling', 'target']     # sibling is /home/me/myXproject/src
  fix  -> ['target']

prune_sessions() deletes the rows this clause matches, along with their on-disk transcripts, so pruning one project's sessions takes an unrelated project's history with it. list_prune_candidates (which backs --dry-run and the confirmation preview) runs through the same builder, so the preview shows the same over-broad set rather than warning about it.

The bare-% case is the same class from the other direction — a prefix of /home/me/% matches every session under that root.

The fix

Escape the needle and pair both LIKE arms with ESCAPE '\' — the convention the rest of hermes_state.py already follows for user-derived text. The literal separator backslash in the Windows pattern is escaped for the same reason. The = arm is an exact compare and keeps the raw prefix.

Directory-and-children matching is unchanged for prefixes without wildcards: the clause still matches the directory itself and everything below it.

This is the follow-up I flagged in #78681, which escaped the title_like / model_like / branch_like filters and deliberately left this helper alone because it is shared by four call sites beyond prune. The two changes touch hermes_state.py in unrelated regions and do not conflict.

Tests

Added to TestPruneSessionFilters in tests/test_hermes_state.py:

  • a prefix containing _ selects only the real directory, and prune_sessions deletes exactly one session — the same-length sibling survives
  • a bare % prefix selects nothing
  • the prefix still matches the directory itself and its children, and not a neighbour — the guard against over-escaping

Results:

181 passed

That is the whole file; the 178 pre-existing tests are unchanged and there are no pre-existing failures in it.

Red without the source change:

FAILED TestPruneSessionFilters::test_cwd_prefix_underscore_is_literal_not_a_wildcard
E   AssertionError: assert {'sibling', 'target'} == {'target'}
1 failed, 2 passed

The two control cases pass on both sides, which is what they are for.

_cwd_prefix_clause builds "cwd is this directory or under it" for session
listing, workspace resume and prune/archive. The two LIKE arms bound the
raw prefix, so `_` and `%` acted as wildcards on a value that is a path:

  cwd_prefix="/home/me/my_project"
    main -> ['sibling', 'target']    # /home/me/myXproject/src matched too
    fix  -> ['target']

`_` matches any single character, so a same-length sibling directory with
children falls inside the pattern. prune_sessions() deletes the rows it
matches (and their on-disk transcripts), so an unrelated project's history
goes with it.

Escape the needle and pair both arms with ESCAPE, the convention the rest
of this file already uses; the literal separator backslash in the Windows
pattern is escaped for the same reason. The `=` arm is an exact compare and
keeps the raw prefix, so directory-and-children matching is unchanged.

Follow-up to the *_like filters in NousResearch#78681, kept separate because this helper
is shared by four call sites beyond prune.
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint area/sessions Session lifecycle, resume, persistence, history P1 High — major feature broken, no workaround sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state labels Aug 4, 2026
kshitijk4poor added a commit that referenced this pull request Aug 5, 2026
Follow-up on the salvage of #78681 + #78927: the second fix inlined the
exact body of the _escape_like helper the first fix introduced ten lines
above. Call the helper instead so there is one copy of the escaping rule.
@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Merged via #79722 — your commit cherry-picked onto current main with authorship preserved (rebase merge), together with #78681. Follow-up we added: the inline escape in _cwd_prefix_clause now calls the _escape_like helper from the first fix, plus a regression test for the Windows backslash child arm. Thanks for flagging and finishing the whole bug class!

randlee pushed a commit to randlee/hermes-agent that referenced this pull request Aug 11, 2026
Follow-up on the salvage of NousResearch#78681 + NousResearch#78927: the second fix inlined the
exact body of the _escape_like helper the first fix introduced ten lines
above. Call the helper instead so there is one copy of the escaping rule.
33hodl pushed a commit to 33hodl/hermes-agent that referenced this pull request Aug 12, 2026
Follow-up on the salvage of NousResearch#78681 + NousResearch#78927: the second fix inlined the
exact body of the _escape_like helper the first fix introduced ten lines
above. Call the helper instead so there is one copy of the escaping rule.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sessions Session lifecycle, resume, persistence, history comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P1 High — major feature broken, no workaround sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants