Skip to content

feat(sessions): drop empty sessions on CLI exit and rotation (port from gemini-cli#27770) - #43855

Merged
2 commits merged into
mainfrom
gemini-cli-port/empty-session-hygiene
Jun 11, 2026
Merged

feat(sessions): drop empty sessions on CLI exit and rotation (port from gemini-cli#27770)#43855
2 commits merged into
mainfrom
gemini-cli-port/empty-session-hygiene

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Summary

Empty sessions no longer pile up in /resume, hermes sessions list, and the in-chat recent-sessions browser: starting the CLI and immediately quitting (or rotating with /new / /clear) now drops the untitled, message-less session row instead of persisting it.

Port of google-gemini/gemini-cli#27770 ("Avoid persisting empty resume sessions"), adapted to hermes-agent's SQLite SessionDB.

Changes

  • hermes_state.py: new SessionDB.delete_session_if_empty() — transactional check-and-delete that only removes rows with no messages, no title, and no child sessions (a parent that spawned delegate subagent runs is never considered empty). Reuses _remove_session_files for on-disk transcript cleanup.
  • cli.py: _discard_session_if_empty() wrapper wired into two paths:
    • the cli_close shutdown path (skipped when /exit --delete already handles removal)
    • new_session() rotation (/new, /clear)
  • tests/test_empty_session_hygiene.py: 13 tests covering delete/keep matrix (messages, title, children), transcript file cleanup, listing visibility, and CLI wiring edge cases (no DB, None id, DB error swallowed).

Complementary to the existing one-shot prune_empty_ghost_sessions migration (TUI-only, >24h-old rows): this prevents new ghost rows at the moment they would be created rather than sweeping them later.

Validation

Result
tests/test_empty_session_hygiene.py 13/13 pass
Regression: test_hermes_state.py, test_exit_delete_session.py, test_lazy_session_regressions.py 291/291 pass
E2E (real SessionDB, temp HERMES_HOME) start-and-quit session deleted; session with messages kept; listing shows only the real session

Source PR: google-gemini/gemini-cli#27770

Port from google-gemini/gemini-cli#27770: starting the CLI and
immediately quitting (or rotating with /new, /clear) left an empty
untitled session row behind. These ghost rows pile up in /resume,
`hermes sessions list`, and the in-chat recent-sessions browser.

- SessionDB.delete_session_if_empty(): transactional check-and-delete
  that only removes rows with no messages, no title, and no child
  sessions (delegate subagent parents are preserved). Also removes
  on-disk transcript files via the existing _remove_session_files.
- HermesCLI._discard_session_if_empty(): thin wrapper, wired into the
  cli_close shutdown path and the new_session() rotation path.
  Skipped when /exit --delete already handles removal.

Unlike the one-shot prune_empty_ghost_sessions migration (TUI-only,
24h-old rows), this prevents new ghost rows from accumulating at the
moment they would be created.
@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

🔎 Lint report: gemini-cli-port/empty-session-hygiene vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 10648 on HEAD, 10647 on base (🆕 +1)

🆕 New issues (1):

Rule Count
unresolved-import 1
First entries
tests/test_empty_session_hygiene.py:10: [unresolved-import] unresolved-import: Cannot resolve imported module `pytest`

✅ Fixed issues: none

Unchanged: 5560 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

CI caught tests/cli/test_cli_new_session.py asserting that /new keeps
the old session row when conversation history exists in memory. The
live transcript is authoritative: a session whose messages haven't
flushed to the DB yet (or whose flush failed) must not be pruned.
Guard _discard_session_if_empty on self.conversation_history and pin
the behavior with a test.
@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/cli CLI entry point, hermes_cli/, setup wizard labels Jun 11, 2026
@teknium1 teknium1 closed this pull request by merging all changes into main in 85503dc Jun 11, 2026
@teknium1
teknium1 deleted the gemini-cli-port/empty-session-hygiene branch June 11, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants