Memory System¶
MemoryQuery¶
polymathera.colony.agents.patterns.memory.types.MemoryQuery
¶
Bases: BaseModel
Query parameters for recalling memories.
Used by MemoryCapability.recall() and AgentContextEngine.gather_context().
Supports three query modes based on which fields are populated:
- Semantic: query text for vector similarity search
- Logical: tag_filter, time_range, key_pattern for structured filtering
- Hybrid: both semantic + logical (results are filtered then ranked by similarity)
The LLM planner constructs MemoryQuery objects. Use list_tags action to
discover available tags before constructing tag-based queries.
Example
# Semantic query
MemoryQuery(query="What authentication approach was used?")
# Logical query by tags
MemoryQuery(tag_filter=TagFilter(all_of={"action", "success"}))
# Hybrid: semantic + tag filter
MemoryQuery(
query="security analysis results",
tag_filter=TagFilter(any_of={"action_type:infer", "action_type:plan"}),
max_results=10,
)
TagFilter¶
polymathera.colony.agents.patterns.memory.types.TagFilter
¶
Bases: BaseModel
Logical filter expression over entry tags.
Supports AND (all_of), OR (any_of), NOT (none_of) combinators. The LLM planner constructs these to do precise tag-based retrieval.
Example
matches(entry_tags)
¶
Check if a set of entry tags satisfies this filter.
Source code in src/polymathera/colony/agents/patterns/memory/types.py
MemoryCapability¶
polymathera.colony.agents.patterns.memory.capability.MemoryCapability(agent, scope_id, *, producers=None, ingestion_policy=None, ttl_seconds=None, max_entries=None, storage_backend_factory=None, retrieval_strategy=None, lenses=None, maintenance_policies=None, maintenance_interval_seconds=60.0, utility_scorer=None, maintenance=None, map_to_vcm=False, vcm_config=None, capability_key=None)
¶
Bases: AgentCapability
Unified memory capability: ingestion + storage + maintenance (cognitive processes).
A MemoryCapability manages ONE memory scope. It:
1. INGESTS data from sources (subscriptions + producers)
- Zero or more producers (hook-based capture of memory-producing methods)
- Zero or more subscriptions (pull/push from other scopes)
2. Transforms ALL inputs together via ingestion_policy.transformer
3. Writes transformed data to THIS scope
4. MAINTAINS this scope in background cognitive processes (decay, prune, dedupe, consolidation)
5. Provides conscious LLM-plannable actions (store, recall, forget, transfer)
6. Provides query interface for recall
Pull model: if another scope wants data from this scope, it subscribes. No push/export logic. Each capability manages its own scope.
This pull model (observer pattern) is motivated by evolutionary history of the brain. - A new memory layer evolves to observe and consolidate data from existing layers. - An existing memory layer does not have to be aware of new layers, which makes the memory system more extensible.
The producers parameter enables hook-based memory capture: the memory
capability registers AFTER hooks on specified methods to automatically
capture their outputs. This implements the principle: "memory is an
observer of agent behavior."
Attributes:
| Name | Type | Description |
|---|---|---|
scope_id |
Blackboard scope for this memory level |
|
producers |
Hook-based memory capture configs |
|
ingestion_policy |
Ingestion policy including memory scopes to listen to and pull data from, and when to trigger ingestion (default: OnDemand) and how to consolidate the entries. |
|
ttl_seconds |
Default TTL for stored memories |
|
max_entries |
Maximum entries before eviction |
|
retrieval |
RetrievalStrategy
|
Strategy for memory retrieval |
lenses |
dict[str, MemoryLens]
|
Named query configurations |
maintenance_policies |
list[MaintenancePolicy]
|
List of maintenance policies |
utility_scorer |
UtilityScorer | None
|
Scorer for memory utility (SEDM-inspired) |
Initialize memory capability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
'Agent'
|
Agent that owns this capability |
required |
scope_id
|
str
|
Blackboard scope for this memory level |
required |
producers
|
list[MemoryProducerConfig] | None
|
Hook-based memory capture configurations |
None
|
ingestion_policy
|
MemoryIngestPolicy | None
|
Dataflow edges from other scopes, and when to trigger ingestion (default: OnDemand) and how to consolidate the entries. Each subscription listens and collects entries into pending queue. ALL pending entries are then transformed together before writing to this scope. If transformer=None, entries stored as-is. |
None
|
ttl_seconds
|
float | None
|
Default TTL for stored memories (None = no expiration) |
None
|
max_entries
|
int | None
|
Maximum entries before eviction (None = no limit) |
None
|
storage_backend_factory
|
StorageBackendFactory | None
|
Factory for creating storage backends for any scope (e.g., this scope or other scopes). Defaults to BlackboardStorageBackendFactory. |
None
|
retrieval_strategy
|
RetrievalStrategy | None
|
Strategy for memory retrieval (default: RecencyRetrieval) |
None
|
lenses
|
list[MemoryLens] | None
|
Named query configurations |
None
|
maintenance_policies
|
list[MaintenancePolicy] | None
|
List of maintenance policies to run Use ConsolidationMaintenancePolicy for within-scope consolidation. |
None
|
maintenance_interval_seconds
|
float
|
How often to run maintenance (default: 60s) |
60.0
|
utility_scorer
|
UtilityScorer | None
|
Scorer for memory utility |
None
|
maintenance
|
MaintenanceConfig | None
|
Legacy MaintenanceConfig (deprecated, use maintenance_policies) |
None
|
map_to_vcm
|
bool
|
If True, map this scope into VCM pages during initialize(). Enables attention-based discovery of this scope's contents. |
False
|
vcm_config
|
MmapConfig | None
|
Configuration for VCM mapping (controls flushing, locality, etc.) Only used if map_to_vcm=True. Defaults to MmapConfig() if None. |
None
|
capability_key
|
str | None
|
Override for the capability dict key (allows multiple instances of MemoryCapability with distinct keys). |
None
|
Source code in src/polymathera/colony/agents/patterns/memory/capability.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
recall(query=None, lens=None, context=None)
async
¶
Recall memories (conscious read).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
MemoryQuery | str | None
|
Query string or MemoryQuery object. LLM planners typically pass a plain string which is auto-wrapped. |
None
|
lens
|
str | None
|
Name of a predefined lens to apply |
None
|
context
|
RetrievalContext | None
|
Retrieval context (goal, agent state) for relevance |
None
|
Returns:
| Type | Description |
|---|---|
list[BlackboardEntry]
|
List of matching BlackboardEntry objects |
Source code in src/polymathera/colony/agents/patterns/memory/capability.py
recall_with_scores(query=None, lens=None, context=None)
async
¶
Recall memories (conscious read) with detailed scoring for debugging/analysis.
This action is exposed to the agent action policy for memory retrieval. The agent can search for relevant memories using semantic similarity, tags, or recency filtering.
Supports: - Query-based filtering (tags, recency, etc.) - Lens-based views (predefined query configurations) - Goal-aware retrieval via RetrievalContext
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
MemoryQuery | str | None
|
Query string or MemoryQuery object for filtering/ranking. LLM planners typically pass a plain string which is auto-wrapped. |
None
|
lens
|
str | None
|
Name of a predefined lens to apply |
None
|
context
|
RetrievalContext | None
|
Retrieval context (goal, agent state) for relevance |
None
|
Returns:
| Type | Description |
|---|---|
list[ScoredEntry]
|
List of matching ScoredEntry objects with score breakdowns |
Source code in src/polymathera/colony/agents/patterns/memory/capability.py
list_tags()
async
¶
List all unique tags and their counts in this scope.
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dict mapping tag name to entry count, sorted by count descending. |
Source code in src/polymathera/colony/agents/patterns/memory/capability.py
stats()
async
¶
Get statistics about stored memories.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with entry_count, unique_tags, oldest/newest entry age, |
dict[str, Any]
|
and top 20 tags by count. |