fix(sessions): align prune filter derivation

This commit is contained in:
fangliquanflq
2026-08-16 14:30:28 +08:00
committed by Teknium
parent 0b8a09759c
commit 0a42bc7113
3 changed files with 13 additions and 35 deletions

View File

@@ -942,13 +942,18 @@ def cmd_sessions(args, sessions_parser=None):
filters["archived"] = False
candidates = db.list_prune_candidates(**filters)
skipped_open = db.count_open_prune_matches(**filters)
# Archive expands each selected row to its compression lineage, which
# can include open continuations; a direct-open count would therefore
# describe the eventual archive effect inaccurately.
skipped_open = (
db.count_open_prune_matches(**filters) if action == "prune" else 0
)
if skipped_open:
suffix = "" if skipped_open == 1 else "s"
print(
f"Note: {skipped_open} open session{suffix} also match these "
f"filters but will be skipped because bulk {action} only "
"affects ended sessions. Use `hermes sessions delete <id>` "
"filters but will be skipped because prune only deletes ended "
"sessions. Use `hermes sessions delete <id>` "
"to remove one explicitly."
)
verb = "Delete" if action == "prune" else "Archive"

View File

@@ -11845,14 +11845,7 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
``request_dump_*``) for every pruned session, outside the DB
transaction.
"""
if (
filters.get("last_active_before") is None
and filters.get("started_before") is None
and older_than_days is not None
):
filters["last_active_before"] = time.time() - (
older_than_days * 86400
)
self._apply_prune_age_filter(older_than_days, filters)
where, where_params = self._prune_filter_where(source=source, **filters)
removed_ids: list[str] = []

View File

@@ -39,10 +39,8 @@ def test_sessions_delete_accepts_unique_id_prefix(monkeypatch, capsys):
assert "Deleted session '20260315_092437_c9a6ff'." in output
def _run_prune(
monkeypatch, capsys, argv_tail, candidates=None, skipped_open=0, action="prune"
):
"""Run `hermes sessions <action> <argv_tail>` against a FakeDB, capturing
def _run_prune(monkeypatch, capsys, argv_tail, candidates=None, skipped_open=0):
"""Run `hermes sessions prune <argv_tail>` against a FakeDB, capturing
the filter kwargs passed to list_prune_candidates. Auto-confirms."""
import hermes_cli.main as main_mod
import hermes_state
@@ -83,15 +81,12 @@ def _run_prune(
def prune_sessions(self, **kwargs):
return len(rows)
def archive_sessions(self, **kwargs):
return len(rows)
def close(self):
pass
monkeypatch.setattr(hermes_state, "SessionDB", lambda: FakeDB())
monkeypatch.setattr(
sys, "argv", ["hermes", "sessions", action, *argv_tail]
sys, "argv", ["hermes", "sessions", "prune", *argv_tail]
)
monkeypatch.setattr("builtins.input", lambda _prompt="": "y")
main_mod.main()
@@ -129,21 +124,6 @@ def test_sessions_prune_surfaces_matching_open_sessions(monkeypatch, capsys):
)
assert "2 open sessions also match these filters" in out
assert "bulk prune only affects ended sessions" in out
assert "prune only deletes ended sessions" in out
assert "hermes sessions delete <id>" in out
assert "No sessions match" in out
def test_sessions_archive_surfaces_matching_open_sessions(monkeypatch, capsys):
_filters, out = _run_prune(
monkeypatch,
capsys,
["--source", "cron"],
candidates=[],
skipped_open=1,
action="archive",
)
assert "1 open session also match these filters" in out
assert "bulk archive only affects ended sessions" in out
assert "No sessions match" in out