Skip to content

user_store

user_store

Per-user Agent registry with LRU eviction and TTL.

Manages a pool of Agent instances (max 10 by default), evicting stale or least-recently-used agents to bound memory. Thread-safe via asyncio.Lock.

AgentRegistry

LRU cache of per-user Agent instances with TTL eviction.

Thread-safe via asyncio.Lock. Agents are evicted after ttl_seconds of inactivity (default 30 min) or when max_size is exceeded (LRU eviction).

__init__

__init__(
    embedder: Embedder,
    kb: KnowledgeBase,
    max_size: int = 10,
    ttl_seconds: int = 1800,
)

Initialize the agent registry.

Parameters:

Name Type Description Default
embedder Embedder

Shared Embedder instance for RAG retrieval.

required
kb KnowledgeBase

Shared KnowledgeBase instance (may be unloaded).

required
max_size int

Maximum number of agents to retain (LRU eviction).

10
ttl_seconds int

Time-to-live in seconds for idle agents.

1800

get_or_create async

get_or_create(
    user_id: str,
    axes: dict | None = None,
    keys: dict[str, str | None] | None = None,
) -> Agent

Get existing agent for user_id, or create a new one.

If the user already has an agent
  • pop from OrderedDict, update timestamp, push to end (MRU).
  • optionally update axes.

Otherwise: - sweep stale entries (TTL eviction). - if at capacity, evict LRU entry (popitem(last=False)). - create new Agent with shared embedder/KB and per-user keys. All operations under async with self._lock.

remove

remove(user_id: str) -> None

Remove an agent from the registry.

No-op if the user ID is not found.

Parameters:

Name Type Description Default
user_id str

The user's UUID to evict from the registry.

required

get_agent_count

get_agent_count() -> int

Return the number of agents currently in the registry.

Returns:

Type Description
int

Integer count of active agent instances.