Large sessions: ~5s compact_and_merge on every /api/session poll holds session lock -> send/steer timeouts, spurious not_running
Summary
With one large session (multi-MB transcript, ~16k+ journal entries), every GET /api/session spends ~5 s in compact_and_merge (regeneration_state → reconciled_state_db_messages_for_session → merge_session_messages_append_only) while holding the session lock. The open tab polls this endpoint continuously, so the lock is saturated: sends and steers wait 13–30 s, requests pile up, RSS grows to ~3.5 GB, and the frontend starts showing timeouts and steer_fail_not_running while a run is in fact active.
Evidence (production traces, 2026-09-29)
59 slow-request records before 09:00Z. Two representative stage breakdowns:
POST /api/chat/start elapsed 29 725 ms
get_session 2 122 ms session_lock_wait 20 944 ms
POST /api/chat/start elapsed 34 939 ms
get_session 9 026 ms session_lock_wait 13 015 ms save_pending_state 8 064 ms
GET /api/session elapsed ~6 189 ms
t1_after_get_session_check 648 ms
t2_after_state_db_load 430 ms
t4_after_compact_and_merge 4 825 ms ← dominant, on every pollThread stack captured by the request watchdog during the slow window:
routes.py:14052 _handle_session_get
session_ops.py:363 regeneration_state
models.py:12898 reconciled_state_db_messages_for_session
models.py:12288 _merge_session_messages_append_only_impl
models.py:12175 _cached_message_key("visible_sidecar")
models.py:11462 _normalized_session_message_content ← " ".join(str(content).split()) over every messageThe merge/cache-key layer re-normalizes full message content on every request; there is no cross-request memoization keyed by journal mtime/size. Under this load:
POST /api/chat/steersometimes resolves the run as not steerable (fallbacknot_running) because state is stale by the time the lock is acquired → user-visible "Agent is not currently running" although the stream is alive.- RSS grows from 1.9 GB → 3.5 GB within a day with 3 concurrent streams.
Impact
Large sessions become effectively unusable: every tab focus/switch triggers seconds of lock-held CPU work, blocking sends/steers (which the user experiences as "Request timed out" / "Agent is not running"). The only local mitigations are closing the tab or restarting — killing active runs.
Suggested directions
- Memoize the canonical/merged state per session keyed by (journal size, mtime_ns, message count) — the building blocks already exist for the static-file cache. Invalidate on
save_pending_state/turn-journal append. - Keep
compact_and_mergeoff the hot path for poll-style requests: serve metadata + tail window from the persisted state DB and only build canonical state on explicit reload/branch ops. - Narrow the lock scope: the 5 s merge should not run while holding the same lock that
/api/chat/start'ssession_lock_waitwaits on. - Bound
_normalized_session_message_contentwork with a per-message memo (message objects are append-only; re-normalizing immutable content every request is pure re-work).
Related user-side fixes already upstreamed from this instance: #7840 (draft worker coalescing), #7888 (steer queue wiped on refresh). Happy to test candidate patches against the affected session (backup available, ~16 MB transcript).
Source: nesquena/hermes-webui