Skip to content

bug: incorrect pagination in Memory Session Manager list_messages #346

Open

Description

@Hweinstock

Problem

Before PR #244

State events and conversational events were naturally separated by actorId:

# create_session used a prefixed actorId
create_event(actorId="session_my-session-id", payload=[{"blob": ...}])

# create_agent used a prefixed actorId
create_event(actorId="agent_my-agent-id", payload=[{"blob": ...}])

# create_message used the regular actorId
create_event(actorId="my-actor-id", payload=[{"conversational": ...}])

So when list_messages called list_events with actorId="my-actor-id", it only got conversational events back. State events lived under different actorIds and were invisible to this query. max_results counted only
what mattered.

After PR #244

Everything now uses the same actorId, with metadata distinguishing state events:

# create_session — same actorId, metadata marks it as state
create_event(
    actorId="my-actor-id",  # ← same as messages now
    metadata={"stateType": {"stringValue": "SESSION"}},
    payload=[{"blob": ...}],
)


# create_agent — same actorId, metadata marks it as state
create_event(
    actorId="my-actor-id",  # ← same as messages now
    metadata={"stateType": {"stringValue": "AGENT"}, "agentId": {"stringValue": "..."}},
    payload=[{"blob": ...}],
)

# create_message — same actorId, no metadata
create_event(actorId="my-actor-id", payload=[{"conversational": ...}])

Potential Issues

list_messages in session_manager.py fetches events without any metadata filter:

events = self.memory_client.list_events(
    memory_id=self.config.memory_id,
    actor_id=self.config.actor_id,
    session_id=session_id,
    max_results=max_results,  # ← this now includes state events in the count
)

Since all events share the same actorId now, this query returns both conversational AND state events. The API returned max_results events total, some of which were state events. Those get discarded by the converter, so the final list is shorter than what the caller asked for.

Concrete example

A session with 1 session state event, 2 agent state events, and 20 conversational messages. Customer calls list_messages(limit=10):

  1. max_results = limit + offset = 10
  2. list_events returns 10 events: 3 state + 7 conversational
  3. events_to_messages discards the 3 state events → 7 messages
  4. Customer gets 7 messages, expected 10

Customer does not get a pagination token, so they assume 7 is all that exists, and doesn't know there are more.

Acceptance Criteria

  • customer should be given nextToken, or should always get maxResults exactly if more results exist.
  • unit tests and/or integration verifying fix of the bug.

Activity

  1. self-assigned this
    on Mar 16, 2026
  2. added
    bug
    on Mar 16, 2026
  3. removed their assignment
    on Jul 10, 2026
  4. hiSandog commented on Jul 14, 2026

    @hiSandog

    For “bug: incorrect pagination in Memory Session Manager 'list_messages'”, the list_messages reproduction could become a table-driven test covering an empty result, one full page, and a final partial page. That would pin down cursor termination as well as the reported failure at the page boundary.

  5. ekumar96 commented on Oct 30, 2026

    @ekumar96

    We hit a related, more severe case of this on the no-limit path that Strands uses for every restore, so I'm adding it here instead of opening a new issue.

    RepositorySessionManager.initialize calls list_messages(session_id, agent_id, offset=conversation_manager.removed_message_count) with no limit. On main (3707a9f, v1.25.2):

    • session_manager.py:785: max_results = (limit + offset) if limit else MAX_FETCH_ALL_RESULTS, so the read is capped at the newest 10,000 events, counting SESSION/AGENT state events as described above.
    • session_manager.py:796-799: messages[offset:] is applied to that capped window, not to the full history. Strands (and FileSessionManager) treat offset as an index into the full message list.
    • session_manager.py:801-803: any exception is logged and returned as [], so a failed read looks like an empty session.

    Impact, reproduced live (Strands agent restoring from AgentCore Memory, Claude Sonnet 4.6 on Bedrock; sessions with 10,100 messages = 10,102 events; a 9,600-message control session restored correctly):

    • NullConversationManager: the restore returns the newest 10,000 messages, so the oldest are dropped silently. The agent then sends more than 10,000 messages and Bedrock rejects every turn with a misleading ValidationException: The additional field role conflicts with an existing field. Each failed turn still journals its user message, so the session can't recover.
    • SlidingWindowConversationManager(150) (stored removed_message_count 9,951): the restore returns 47 messages instead of 150, with no error. That's roughly 150 − (events − 10,000), so from about 10,150 events on it returns zero messages and the agent silently loses all context.

    Minimal repro

    cfg = AgentCoreMemoryConfig(memory_id=MEMORY_ID, session_id="long-1", actor_id="actor-1")
    m = AgentCoreMemorySessionManager(cfg, region_name="us-west-2")
    m.create_session(Session(session_id="long-1", session_type=SessionType.AGENT))
    m.create_agent("long-1", SessionAgent(agent_id="default", state={}, conversation_manager_state=NullConversationManager().get_state()))
    for i in range(10_100):
        m.create_message("long-1", "default", SessionMessage.from_message(
            {"role": "user" if i % 2 == 0 else "assistant", "content": [{"text": f"turn {i // 2}"}]}, i))
    m2 = AgentCoreMemorySessionManager(cfg, region_name="us-west-2")
    len(m2.list_messages("long-1", "default"))               # 10000, expected 10100
    len(m2.list_messages("long-1", "default", offset=9951))  # 49, expected 149

    Suggested fix

    1. With limit is None, paginate all events and apply offset to the full message list. If a safety cap is needed, raise when it's hit instead of truncating.
    2. Count converted messages, not events, when deciding when to stop (this is what fix(memory): paginate list_messages by converted message count, not raw event count #347 does for the limit case).
    3. Re-raise read errors (e.g. as SessionException) instead of returning [].
    4. Add a unit test with more than MAX_FETCH_ALL_RESULTS events and a nonzero offset. Also, the list_events docstring says "chronological order", but the results come back newest first.

    A session gets there after roughly 3,300 short turns, and much sooner for agent loops with many tool calls.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions