Skip to content

fix(sessions): preserve recently active sessions during pruning - #71043

Closed
Frowtek wants to merge 1 commit into
NousResearch:mainfrom
Frowtek:fix/session-retention-last-activity
Closed

fix(sessions): preserve recently active sessions during pruning#71043
Frowtek wants to merge 1 commit into
NousResearch:mainfrom
Frowtek:fix/session-retention-last-activity

Conversation

@Frowtek

@Frowtek Frowtek commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Prevents session retention from deleting long-lived conversations that were active recently.

prune_sessions(older_than_days=...) previously compared the retention cutoff against sessions.started_at. As a result, an ended conversation created more than 90 days ago but used today was silently deleted at CLI/gateway startup when sessions.auto_prune was enabled. This removed the session row, its messages, and transcript files; the normal follow-up VACUUM could make recovery impractical.

Retention age now uses the latest persisted message timestamp, falling back to started_at for empty sessions. Explicit --before and --after filters continue to target session creation time, while relative --older-than and --newer-than filters target activity.

The preview, confirmation output, dashboard response, documentation, and deletion path use the same activity contract.

Related Issue

No issue filed.

Related but non-duplicate work:

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • Updated hermes_state.py so age-based preview and pruning use latest message activity.
  • Kept explicit session-start filters separate from relative activity filters.
  • Updated CLI confirmation and dashboard dry-run output to expose last_active.
  • Added real SQLite regression coverage for DB rows, messages, and transcript preservation.
  • Updated CLI/filter tests and session-retention documentation.

How to Test

  1. On main, create an ended session whose started_at is 100 days old, append a message with the current timestamp, and run:
    maybe_auto_prune_and_vacuum(retention_days=90, vacuum=False, sessions_dir=...)
  2. Observe that main@7cd48733d returns pruned: 1 and deletes the session, messages, and transcript.
  3. Repeat on this branch and observe pruned: 0; the session, message, and transcript remain.
  4. Run:
    .\.venv\Scripts\python.exe -m pytest -o "addopts=-m 'not integration'" -p no:timeout tests/test_hermes_state.py tests/hermes_cli/test_session_filters.py tests/hermes_cli/test_sessions_delete.py tests/hermes_cli/test_sessions_export_md_cli.py tests/hermes_cli/test_dashboard_admin_endpoints.py -q
    Result: 574 passed.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: my locally

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — website/docs/user-guide/sessions.md
  • I've updated cli-config.yaml.example if I added/changed config keys — N/A, no config keys changed
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide
  • I've updated tool descriptions/schemas if I changed tool behavior — N/A

Screenshots / Logs

Current main:

{"started_days_ago":100,"latest_message_age_seconds":0,"maintenance":{"skipped":false,"pruned":1,"vacuumed":false},"session_after":null,"transcript_exists":false}

This branch:

{"started_days_ago":100,"latest_message_age_seconds":0,"maintenance":{"skipped":false,"pruned":0,"vacuumed":false},"session_survived":true,"messages":1,"transcript_exists":true}

Regression suite:

574 passed in 74.09s

@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/gateway Gateway runner, session dispatch, delivery comp/cli CLI entry point, hermes_cli/, setup wizard area/sessions Session lifecycle, resume, persistence, history area/config Config system, migrations, profiles sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jul 24, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Merged via #72358 with your commit cherry-picked onto current main — authorship preserved. Thanks for the prune-by-inactivity fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles area/sessions Session lifecycle, resume, persistence, history comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery P1 High — major feature broken, no workaround sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages 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