feat: opt-in state.db sync for /insights visibility (#92)#93
feat: opt-in state.db sync for /insights visibility (#92)#93nesquena-hermes merged 1 commit intomasterfrom
Conversation
WebUI sessions were invisible to 'hermes /insights' because the WebUI bypasses the gateway and calls AIAgent.run_conversation() directly, never writing to state.db. New 'Sync usage to /insights' setting (default: off) that mirrors WebUI session metadata (tokens, cost, model, title) into state.db after each turn. Uses absolute token counts to avoid double-counting. Components: - api/state_sync.py: bridge module with sync_session_start() and sync_session_usage(). Uses ensure_session() (idempotent) and update_token_counts(absolute=True). All wrapped in try/except. - api/config.py: new 'sync_to_insights' boolean setting - api/streaming.py: calls sync_session_usage() after s.save() - api/routes.py: same for the non-streaming chat path - Settings UI: checkbox toggle with description Default off because: - Writing to state.db while CLI/gateway also writes could cause WAL lock contention on busy systems - Some users may not want WebUI sessions in /insights stats Closes #92 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Agent review — APPROVED WITH FIXES ✅ Full diff review, security audit, correctness audit against the live hermes-agent codebase, and test run completed. What this PR does: Adds an opt-in setting ("Sync usage to /insights") that mirrors WebUI session token usage and titles into state.db after each turn, so Diff: 6 files, 134 insertions, 1 deletion Fixes applied on pr-93-review branch: Three bugs found by auditing the actual
The rest of the design is solid: |
Summary
Fixes #92 -- WebUI sessions are invisible to
hermes /insightsbecause the WebUI callsAIAgent.run_conversation()directly, bypassing the gateway that normally writes to state.db.Approach
New opt-in setting "Sync usage to /insights" (default: off). When enabled, after each turn completes the WebUI mirrors session metadata (tokens, cost, model, title) into state.db so
/insightsincludes browser session data.Why default off?
/insightsstatsComponents
api/state_sync.pysync_session_usage()usingensure_session()+update_token_counts(absolute=True)api/config.pysync_to_insightsboolean setting added to defaultsapi/streaming.pysync_session_usage()afters.save()(streaming path)api/routes.pystatic/index.htmlstatic/panels.jsSafety
absolute=Trueonupdate_token_counts()-- the WebUI Session already accumulates totals, so no double-counting riskensure_session()is idempotent (INSERT OR IGNORE)hermes_statemodule is not importable or state.db doesn't exist, sync silently no-opsTest plan
hermes /insights-- should show the WebUI session/insightsshould not updateGenerated with Claude Code