Skip to content

Gateway sessions reset memory nudge counter, so self-improvement review may never trigger #22357

@aibuddyxxl

Description

@aibuddyxxl

Bug Description

In gateway/messaging-platform usage (observed on Telegram), Hermes appears to create a fresh AIAgent per inbound message while continuing the persisted session history. The periodic self-improvement memory review is gated by the instance variable _turns_since_memory, which is initialized to 0 in AIAgent.__init__.

Because the gateway creates a new agent object per message, _turns_since_memory starts at 0 every turn and memory.nudge_interval may never be reached. In practice this means the background Self-improvement review stops appearing and durable user corrections/workflow lessons are not proactively saved.

This is related to #18369, but the scenario is not only users manually starting /new; it can happen in normal long-running gateway conversations where the conversation history persists but the agent instance does not.

Steps to Reproduce

  1. Configure memory review with the default-ish settings:
memory:
  memory_enabled: true
  user_profile_enabled: true
  nudge_interval: 10
  1. Run Hermes through the gateway (Telegram observed).
  2. Continue a normal conversation for many turns without /new.
  3. Watch for 💾 Self-improvement review: messages or memory/skill background review tool actions.

Expected Behavior

The memory review counter should survive gateway turn boundaries, or be reconstructed from persisted session history, so that memory.nudge_interval still triggers in messaging-platform sessions.

Actual Behavior

The fresh per-message AIAgent resets _turns_since_memory to 0, so the turn-based trigger can be starved indefinitely in gateway mode.

Observed user-facing result:

  • no Self-improvement review messages for a long time despite many turns and obvious durable lessons,
  • important workflow correction was not saved until manually investigated,
  • compact user memory became nearly full, and no automatic memory compaction/Obsidian-style detail fallback occurred.

Environment

  • Hermes Agent: v0.13.0 (2026.5.7)
  • Source checkout: NousResearch/hermes-agent, branch main
  • Platform: Telegram gateway
  • Config snippet:
memory:
  memory_enabled: true
  user_profile_enabled: true
  memory_char_limit: 2200
  user_char_limit: 1375
  nudge_interval: 10
  flush_min_turns: 6
skills:
  creation_nudge_interval: 15

Local Patch Tested

A minimal local fix was tested: hydrate _turns_since_memory from persisted conversation_history when a fresh agent starts with counter 0.

Conceptually:

prior_user_turns = sum(1 for msg in conversation_history if msg.get("role") == "user")
self._turns_since_memory = prior_user_turns % self._memory_nudge_interval

Then the normal per-turn increment can trigger the review when the effective turn count reaches the interval.

Regression test added locally:

tests/run_agent/test_memory_review_counter.py

Result:

3 passed
python -m py_compile run_agent.py  # ok

Notes / Questions

  • This same lifecycle issue may also affect _iters_since_skill and skills.creation_nudge_interval in gateway mode.
  • A more robust upstream solution might persist nudge state in the session DB, derive it from history, or trigger background review on session split/reset.
  • The user expectation is that self-improvement works automatically in messenger mode; users should not have to manually notice that memory is full or that reviews stopped triggering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/agentCore agent loop, run_agent.py, prompt buildercomp/gatewayGateway runner, session dispatch, deliverytool/memoryMemory tool and memory providerstype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions