Skip to content

fix(gateway): invalidate agent cache on cross-process session writes (#45966) - #46095

Closed
kyssta-exe wants to merge 1 commit into
NousResearch:mainfrom
kyssta-exe:auto-fix/issue-45966
Closed

fix(gateway): invalidate agent cache on cross-process session writes (#45966)#46095
kyssta-exe wants to merge 1 commit into
NousResearch:mainfrom
kyssta-exe:auto-fix/issue-45966

Conversation

@kyssta-exe

Copy link
Copy Markdown
Contributor

Fixes #45966. When another process (e.g. hermes dashboard) writes to the same session DB, the cached agent's in-memory transcript becomes stale. This fix stores the session's message_count alongside the cached agent and checks it on reuse. If the count has changed (indicating a cross-process write), the cache entry is discarded and a fresh agent is built from the updated DB transcript.

@liuhao1024

Copy link
Copy Markdown
Contributor

Verification: clean review — agent cache invalidation for cross-process writes

Reviewed the full diff. Implementation is sound:

  1. Staleness detection via message_count — queries _session_db.get_session() before acquiring _agent_cache_lock, avoiding holding the lock during I/O. Correct ordering.
  2. Defensive None handling_current_msg_count can be None (session not found, exception); the if _cached_mc is not None and _current_msg_count is not None guard correctly skips invalidation in that case rather than false-positive evicting.
  3. Cache tuple migration(agent, _sig)(agent, _sig, _current_msg_count) with len(cached) > 2 backward compat for entries cached before this change. No crash on mixed old/new tuples.
  4. Resource cleanup on eviction_cleanup_agent_resources is called on the evicted agent before creating a fresh one, preventing resource leaks from stale agents.
  5. Logginglogger.info with both old and new message_count values gives operators clear visibility into when cross-process writes trigger cache misses.

Checked for: race conditions between the pre-lock DB query and the post-lock cache check (benign — worst case is one unnecessary cache miss), dead variables, callback chain issues. No findings.

Tests cover the happy path (cache hit/miss) and the cross-process invalidation scenario. Clean PR.

@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists duplicate This issue or pull request already exists labels Jun 14, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #45949 — same fix: snapshot the session's on-disk message_count alongside the cached agent and evict+rebuild on cross-process transcript growth. #45949 is the earlier open PR for #45966.

@kyssta-exe

Copy link
Copy Markdown
Contributor Author

This PR's fix for #45966 is already merged via #46237. Closing as superseded.

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

Labels

comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway replies with stale context when another process appends to the same session (cross-process agent-cache split-brain)

3 participants