Class SharedMemoryRegistry

java.lang.Object
com.tnsai.memory.SharedMemoryRegistry

public final class SharedMemoryRegistry extends Object
Registry for shared memory stores across agents.

Manages shared memory namespaces and provides lookup for agents that need to share memory. Used by AgentMemoryResolver when processing @Memory(shared = true, shareWith = {...}).

This is a singleton registry. Each namespace maps to a single SharedMemoryStore instance that multiple agents can access.

See Also:
  • Method Details

    • getInstance

      public static SharedMemoryRegistry getInstance()
      Returns the singleton instance.
      Returns:
      the shared memory registry
    • getOrCreate

      public SharedMemoryStore getOrCreate(String namespace)
      Gets or creates a shared memory store for the given namespace.

      If the namespace already exists, the existing store is returned. Otherwise, a new SharedMemoryStore wrapping an InMemoryStore is created and registered.

      Parameters:
      namespace - the namespace identifier
      Returns:
      the shared memory store for the namespace
    • getOrCreate

      public SharedMemoryStore getOrCreate(String namespace, Supplier<MemoryStore> baseStoreSupplier)
      Gets or creates a shared memory store wrapping a base store from the given supplier.

      The supplier is only invoked when the namespace does not exist yet; for an existing namespace the registered store is returned and the supplier is never called. Callers can therefore pass a supplier that builds an expensive store (e.g. a FileMemoryStore) without wasting instances.

      Parameters:
      namespace - the namespace identifier
      baseStoreSupplier - supplies the base memory store to wrap (used only if namespace is new)
      Returns:
      the shared memory store for the namespace
    • grantAccess

      public void grantAccess(String namespace, String agentId)
      Grants an agent access to a namespace and records the grant in the agent index.

      The recorded index lets AgentMemoryResolver route an agent to the namespaces it was granted via other agents' shareWith lists. The namespace is created with a default InMemoryStore if it does not exist yet; callers that need a specific base store should call getOrCreate(String, Supplier) first.

      Parameters:
      namespace - the namespace to grant access to
      agentId - the agent ID to authorize
    • namespacesGrantedTo

      public Set<String> namespacesGrantedTo(String agentId)
      Returns the namespaces the given agent was granted access to via grantAccess(String, String).
      Parameters:
      agentId - the agent ID to look up
      Returns:
      unmodifiable set of granted namespaces, empty if none
    • find

      public Optional<SharedMemoryStore> find(String namespace)
      Looks up an existing shared memory store by namespace.
      Parameters:
      namespace - the namespace to look up
      Returns:
      the store if it exists
    • remove

      public boolean remove(String namespace)
      Removes a shared memory namespace.
      Parameters:
      namespace - the namespace to remove
      Returns:
      true if the namespace existed and was removed
    • clear

      public void clear()
      Clears all shared memory namespaces and recorded access grants. Primarily useful for testing.
    • size

      public int size()
      Returns the number of active namespaces.
      Returns:
      namespace count