Class MongoAgentStateStore

java.lang.Object
io.agentscope.extensions.mongodb.state.MongoAgentStateStore
All Implemented Interfaces:
io.agentscope.core.state.AgentStateStore, AutoCloseable

public class MongoAgentStateStore extends Object implements io.agentscope.core.state.AgentStateStore, AutoCloseable
MongoDB-backed implementation of AgentStateStore.

Each session is stored as a single MongoDB document with a compound _id of {user, session} to prevent cross-tenant key collisions. State keys are stored inside a states sub-document (via dot notation, e.g. states.foo), keeping them isolated from reserved top-level fields such as _id, user_id, session_id, and _updated_at. Per-key versions live in a versions sub-document and list-content hashes in a hashes sub-document.

Supports optimistic concurrency via the versions.<key> field. List state uses ListHashUtil for change detection to avoid unnecessary full rewrites. The list element count is obtained server-side via $size aggregation to avoid transferring the full array just to call .size().

Usage:


 MongoAgentStateStore store = MongoAgentStateStore.builder()
     .connectionString("mongodb://localhost:27017")
     .databaseName("agentscope")
     .collectionName("sessions")
     .build();
 

Key constraints

  • Method Details

    • builder

      public static MongoAgentStateStore.Builder builder()
    • supportsVersioning

      public boolean supportsVersioning()
      Specified by:
      supportsVersioning in interface io.agentscope.core.state.AgentStateStore
    • save

      public void save(String userId, String sessionId, String key, io.agentscope.core.state.State value)
      Specified by:
      save in interface io.agentscope.core.state.AgentStateStore
    • get

      public <T extends io.agentscope.core.state.State> Optional<T> get(String userId, String sessionId, String key, Class<T> type)
      Specified by:
      get in interface io.agentscope.core.state.AgentStateStore
    • save

      public void save(String userId, String sessionId, String key, List<? extends io.agentscope.core.state.State> values)
      Saves a list of state values with incremental-append optimization.

      The list element count is obtained server-side via $size aggregation so only a single integer is transferred, not the full array. This avoids negating the network savings of incremental append for large conversation histories.

      Concurrency note: this method performs a read-then-write to decide between incremental append and full replacement, so it is intentionally NOT atomic: concurrent writers for the same (userId, sessionId, key) may interleave reads and writes, causing duplicated or lost appends. This matches the harness execution model, where a SandboxExecutionGuard serialises calls per isolation slot, so each session normally has at most one writer at a time. Callers that write the same list concurrently from outside the harness must synchronise externally.

      Specified by:
      save in interface io.agentscope.core.state.AgentStateStore
    • getList

      public <T extends io.agentscope.core.state.State> List<T> getList(String userId, String sessionId, String key, Class<T> itemType)
      Specified by:
      getList in interface io.agentscope.core.state.AgentStateStore
    • getVersioned

      public <T extends io.agentscope.core.state.State> io.agentscope.core.state.VersionedState<T> getVersioned(String userId, String sessionId, String key, Class<T> type)
      Specified by:
      getVersioned in interface io.agentscope.core.state.AgentStateStore
    • saveIfVersion

      public long saveIfVersion(String userId, String sessionId, String key, io.agentscope.core.state.State value, long expectedVersion)
      Specified by:
      saveIfVersion in interface io.agentscope.core.state.AgentStateStore
    • exists

      public boolean exists(String userId, String sessionId)
      Specified by:
      exists in interface io.agentscope.core.state.AgentStateStore
    • delete

      public void delete(String userId, String sessionId)
      Specified by:
      delete in interface io.agentscope.core.state.AgentStateStore
    • delete

      public void delete(String userId, String sessionId, String key)
      Specified by:
      delete in interface io.agentscope.core.state.AgentStateStore
    • listSessionIds

      public Set<String> listSessionIds(String userId)
      Specified by:
      listSessionIds in interface io.agentscope.core.state.AgentStateStore
    • close

      public void close()
      Specified by:
      close in interface io.agentscope.core.state.AgentStateStore
      Specified by:
      close in interface AutoCloseable