Skip to content

fix: inject SessionDB into AIAgent for WebUI sessions (enables session_search) - #356

Closed
DelightRun wants to merge 1 commit into
nesquena:masterfrom
DelightRun:fix/session-search-db-injection
Closed

DelightRun wants to merge 1 commit into
nesquena:masterfrom
DelightRun:fix/session-search-db-injection

Conversation

@DelightRun

Copy link
Copy Markdown

Problem

The session_search tool always returns "Session database not available" when used from WebUI sessions. This means the agent cannot recall past conversations or search session history — a capability that works fine in CLI and gateway modes.

Root Cause

The session_search tool requires a SessionDB instance passed via the session_db parameter on AIAgent.init(). Both the CLI path (cli.py) and gateway path (gateway/run.py) already initialize and inject SessionDB, but the WebUI streaming path (api/streaming.py) was missing this step.

The agent's _invoke_tool() method checks self._session_db — when it's None, it short-circuits with the error message without ever touching the database.

Fix

Two changes in api/streaming.py:

  1. Initialize SessionDB() before creating the AIAgent instance (with try/except so failures are non-fatal — just logs a warning)
  2. Pass session_db=_session_db to the AIAgent() constructor

Testing

Verified manually: after the fix, session_search successfully returns recent sessions with metadata (session_id, title, source, timestamps, preview) from the existing state.db (59 sessions, 2309 messages).

session_search tool requires a SessionDB instance passed via the
session_db parameter. The CLI and gateway paths already do this,
but the WebUI streaming path was missing it, causing every
session_search call to return 'Session database not available'.

Initialize SessionDB before creating the AIAgent and pass it through.
Failure is non-fatal — a warning is printed and session_search
gracefully degrades.
@nesquena-hermes

Copy link
Copy Markdown
Collaborator

Review: fix — inject SessionDB into AIAgent for WebUI sessions

Thanks for this! Clean, minimal fix that closes an important capability gap.

What the fix does

session_search was silently returning "Session database not available" from WebUI sessions because api/streaming.py never initialized or passed a SessionDB instance — even though the CLI and gateway paths already do this. The two-line fix adds the missing initialization and passes it through.

Code review

The change is correct and well-structured:

_session_db = None
try:
    from hermes_state import SessionDB
    _session_db = SessionDB()
except Exception as _db_err:
    print(f"[webui] WARNING: SessionDB init failed — session_search will be unavailable: {_db_err}", flush=True)
  • ✅ Non-fatal fallback — if hermes_state isn't available or SessionDB() raises, the agent still starts and session_search degrades gracefully (same behavior as before this PR)
  • ✅ The flush=True on the warning ensures it shows up immediately in streamed log output
  • session_db=_session_db passed at the correct position in the constructor call — matches the existing CLI/gateway pattern exactly
  • ✅ Scoped inside the streaming generator where _AIAgent is resolved, not at module level — so it follows the same lazy-init pattern as the rest of streaming.py

Minor note

The warning uses print() rather than logger.debug(). Given that PR #354 (currently open) is replacing print/bare-except patterns with logger.debug() throughout the codebase, you may want to align this with that style — e.g.:

logger.warning("SessionDB init failed — session_search will be unavailable: %s", _db_err)

Not a blocker for this PR, just worth noting for consistency.

Summary

Check Status
Root cause analysis ✅ Correct — CLI/gateway inject SessionDB, WebUI didn't
Fix correctness ✅ Minimal, targeted, non-breaking
Graceful degradation None fallback preserves prior behavior on failure
Scope ✅ Single file, 8 lines

Looks good to merge.

nesquena-hermes pushed a commit that referenced this pull request Apr 13, 2026
…n_search) (#356)

- api/streaming.py: initialize SessionDB() before AIAgent construction and
  pass session_db= kwarg so session_search works in WebUI sessions
- tests/test_sprint42.py: 7 new tests covering SessionDB injection, try/except
  guard, WARNING log, ordering, and AST lock-safety check
- CHANGELOG.md: v0.50.13 entry; 822 tests total (up from 815)
nesquena-hermes added a commit that referenced this pull request Apr 13, 2026
…on_search (#356)

* fix: inject SessionDB into AIAgent for WebUI sessions

session_search tool requires a SessionDB instance passed via the
session_db parameter. The CLI and gateway paths already do this,
but the WebUI streaming path was missing it, causing every
session_search call to return 'Session database not available'.

Initialize SessionDB before creating the AIAgent and pass it through.
Failure is non-fatal — a warning is printed and session_search
gracefully degrades.

* fix: inject SessionDB into AIAgent for WebUI sessions (enables session_search) (#356)

- api/streaming.py: initialize SessionDB() before AIAgent construction and
  pass session_db= kwarg so session_search works in WebUI sessions
- tests/test_sprint42.py: 7 new tests covering SessionDB injection, try/except
  guard, WARNING log, ordering, and AST lock-safety check
- CHANGELOG.md: v0.50.13 entry; 822 tests total (up from 815)

---------

Co-authored-by: 王昌旭 <wangchangxu@xiaohongshu.com>
Co-authored-by: Nathan Esquenazi <nesquena@gmail.com>
@nesquena-hermes

Copy link
Copy Markdown
Collaborator

Closed in favor of #359 which merged this fix with added tests (test_sprint42.py) and CHANGELOG entry. 822 tests pass on master.

JKJameson pushed a commit to JKJameson/hermes-webui that referenced this pull request Apr 25, 2026
…on_search (nesquena#356)

* fix: inject SessionDB into AIAgent for WebUI sessions

session_search tool requires a SessionDB instance passed via the
session_db parameter. The CLI and gateway paths already do this,
but the WebUI streaming path was missing it, causing every
session_search call to return 'Session database not available'.

Initialize SessionDB before creating the AIAgent and pass it through.
Failure is non-fatal — a warning is printed and session_search
gracefully degrades.

* fix: inject SessionDB into AIAgent for WebUI sessions (enables session_search) (nesquena#356)

- api/streaming.py: initialize SessionDB() before AIAgent construction and
  pass session_db= kwarg so session_search works in WebUI sessions
- tests/test_sprint42.py: 7 new tests covering SessionDB injection, try/except
  guard, WARNING log, ordering, and AST lock-safety check
- CHANGELOG.md: v0.50.13 entry; 822 tests total (up from 815)

---------

Co-authored-by: 王昌旭 <wangchangxu@xiaohongshu.com>
Co-authored-by: Nathan Esquenazi <nesquena@gmail.com>
SysAdminDoc pushed a commit to SysAdminDoc/hermes-webui that referenced this pull request Jun 26, 2026
…on_search (nesquena#356)

* fix: inject SessionDB into AIAgent for WebUI sessions

session_search tool requires a SessionDB instance passed via the
session_db parameter. The CLI and gateway paths already do this,
but the WebUI streaming path was missing it, causing every
session_search call to return 'Session database not available'.

Initialize SessionDB before creating the AIAgent and pass it through.
Failure is non-fatal — a warning is printed and session_search
gracefully degrades.

* fix: inject SessionDB into AIAgent for WebUI sessions (enables session_search) (nesquena#356)

- api/streaming.py: initialize SessionDB() before AIAgent construction and
  pass session_db= kwarg so session_search works in WebUI sessions
- tests/test_sprint42.py: 7 new tests covering SessionDB injection, try/except
  guard, WARNING log, ordering, and AST lock-safety check
- CHANGELOG.md: v0.50.13 entry; 822 tests total (up from 815)

---------

Co-authored-by: 王昌旭 <wangchangxu@xiaohongshu.com>
Co-authored-by: Nathan Esquenazi <nesquena@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants