alma-memory 0.4.0__py3-none-any.whl → 0.5.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. alma/__init__.py +121 -45
  2. alma/confidence/__init__.py +1 -1
  3. alma/confidence/engine.py +92 -58
  4. alma/confidence/types.py +34 -14
  5. alma/config/loader.py +3 -2
  6. alma/consolidation/__init__.py +23 -0
  7. alma/consolidation/engine.py +678 -0
  8. alma/consolidation/prompts.py +84 -0
  9. alma/core.py +136 -28
  10. alma/domains/__init__.py +6 -6
  11. alma/domains/factory.py +12 -9
  12. alma/domains/schemas.py +17 -3
  13. alma/domains/types.py +8 -4
  14. alma/events/__init__.py +75 -0
  15. alma/events/emitter.py +284 -0
  16. alma/events/storage_mixin.py +246 -0
  17. alma/events/types.py +126 -0
  18. alma/events/webhook.py +425 -0
  19. alma/exceptions.py +49 -0
  20. alma/extraction/__init__.py +31 -0
  21. alma/extraction/auto_learner.py +265 -0
  22. alma/extraction/extractor.py +420 -0
  23. alma/graph/__init__.py +106 -0
  24. alma/graph/backends/__init__.py +32 -0
  25. alma/graph/backends/kuzu.py +624 -0
  26. alma/graph/backends/memgraph.py +432 -0
  27. alma/graph/backends/memory.py +236 -0
  28. alma/graph/backends/neo4j.py +417 -0
  29. alma/graph/base.py +159 -0
  30. alma/graph/extraction.py +198 -0
  31. alma/graph/store.py +860 -0
  32. alma/harness/__init__.py +4 -4
  33. alma/harness/base.py +18 -9
  34. alma/harness/domains.py +27 -11
  35. alma/initializer/__init__.py +1 -1
  36. alma/initializer/initializer.py +51 -43
  37. alma/initializer/types.py +25 -17
  38. alma/integration/__init__.py +9 -9
  39. alma/integration/claude_agents.py +32 -20
  40. alma/integration/helena.py +32 -22
  41. alma/integration/victor.py +57 -33
  42. alma/learning/__init__.py +27 -27
  43. alma/learning/forgetting.py +198 -148
  44. alma/learning/heuristic_extractor.py +40 -24
  45. alma/learning/protocols.py +65 -17
  46. alma/learning/validation.py +7 -2
  47. alma/mcp/__init__.py +4 -4
  48. alma/mcp/__main__.py +2 -1
  49. alma/mcp/resources.py +17 -16
  50. alma/mcp/server.py +102 -44
  51. alma/mcp/tools.py +180 -45
  52. alma/observability/__init__.py +84 -0
  53. alma/observability/config.py +302 -0
  54. alma/observability/logging.py +424 -0
  55. alma/observability/metrics.py +583 -0
  56. alma/observability/tracing.py +440 -0
  57. alma/progress/__init__.py +3 -3
  58. alma/progress/tracker.py +26 -20
  59. alma/progress/types.py +8 -12
  60. alma/py.typed +0 -0
  61. alma/retrieval/__init__.py +11 -11
  62. alma/retrieval/cache.py +20 -21
  63. alma/retrieval/embeddings.py +4 -4
  64. alma/retrieval/engine.py +179 -39
  65. alma/retrieval/scoring.py +73 -63
  66. alma/session/__init__.py +2 -2
  67. alma/session/manager.py +5 -5
  68. alma/session/types.py +5 -4
  69. alma/storage/__init__.py +70 -0
  70. alma/storage/azure_cosmos.py +414 -133
  71. alma/storage/base.py +215 -4
  72. alma/storage/chroma.py +1443 -0
  73. alma/storage/constants.py +103 -0
  74. alma/storage/file_based.py +59 -28
  75. alma/storage/migrations/__init__.py +21 -0
  76. alma/storage/migrations/base.py +321 -0
  77. alma/storage/migrations/runner.py +323 -0
  78. alma/storage/migrations/version_stores.py +337 -0
  79. alma/storage/migrations/versions/__init__.py +11 -0
  80. alma/storage/migrations/versions/v1_0_0.py +373 -0
  81. alma/storage/pinecone.py +1080 -0
  82. alma/storage/postgresql.py +1559 -0
  83. alma/storage/qdrant.py +1306 -0
  84. alma/storage/sqlite_local.py +504 -60
  85. alma/testing/__init__.py +46 -0
  86. alma/testing/factories.py +301 -0
  87. alma/testing/mocks.py +389 -0
  88. alma/types.py +62 -14
  89. alma_memory-0.5.1.dist-info/METADATA +939 -0
  90. alma_memory-0.5.1.dist-info/RECORD +93 -0
  91. {alma_memory-0.4.0.dist-info → alma_memory-0.5.1.dist-info}/WHEEL +1 -1
  92. alma_memory-0.4.0.dist-info/METADATA +0 -488
  93. alma_memory-0.4.0.dist-info/RECORD +0 -52
  94. {alma_memory-0.4.0.dist-info → alma_memory-0.5.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1559 @@
1
+ """
2
+ ALMA PostgreSQL Storage Backend.
3
+
4
+ Production-ready storage using PostgreSQL with pgvector extension for
5
+ native vector similarity search. Supports connection pooling.
6
+
7
+ Recommended for:
8
+ - Customer deployments (Azure PostgreSQL, AWS RDS, etc.)
9
+ - Self-hosted production environments
10
+ - High-availability requirements
11
+ """
12
+
13
+ import json
14
+ import logging
15
+ import os
16
+ from contextlib import contextmanager
17
+ from datetime import datetime, timezone
18
+ from typing import Any, Dict, List, Optional
19
+
20
+ # numpy is optional - only needed for fallback similarity when pgvector unavailable
21
+ try:
22
+ import numpy as np
23
+
24
+ NUMPY_AVAILABLE = True
25
+ except ImportError:
26
+ np = None # type: ignore
27
+ NUMPY_AVAILABLE = False
28
+
29
+ from alma.storage.base import StorageBackend
30
+ from alma.storage.constants import POSTGRESQL_TABLE_NAMES, MemoryType
31
+ from alma.types import (
32
+ AntiPattern,
33
+ DomainKnowledge,
34
+ Heuristic,
35
+ Outcome,
36
+ UserPreference,
37
+ )
38
+
39
+ logger = logging.getLogger(__name__)
40
+
41
+ # Try to import psycopg (v3) with connection pooling
42
+ try:
43
+ from psycopg.rows import dict_row
44
+ from psycopg_pool import ConnectionPool
45
+
46
+ PSYCOPG_AVAILABLE = True
47
+ except ImportError:
48
+ PSYCOPG_AVAILABLE = False
49
+ logger.warning(
50
+ "psycopg not installed. Install with: pip install 'alma-memory[postgres]'"
51
+ )
52
+
53
+
54
+ class PostgreSQLStorage(StorageBackend):
55
+ """
56
+ PostgreSQL storage backend with pgvector support.
57
+
58
+ Uses native PostgreSQL vector operations for efficient similarity search.
59
+ Falls back to application-level cosine similarity if pgvector is not installed.
60
+
61
+ Database schema (uses canonical memory type names with alma_ prefix):
62
+ - alma_heuristics: id, agent, project_id, condition, strategy, ...
63
+ - alma_outcomes: id, agent, project_id, task_type, ...
64
+ - alma_preferences: id, user_id, category, preference, ...
65
+ - alma_domain_knowledge: id, agent, project_id, domain, fact, ...
66
+ - alma_anti_patterns: id, agent, project_id, pattern, ...
67
+
68
+ Vector search:
69
+ - Uses pgvector extension if available
70
+ - Embeddings stored as VECTOR type with cosine distance operator (<=>)
71
+
72
+ Table names are derived from alma.storage.constants.POSTGRESQL_TABLE_NAMES
73
+ for consistency across all storage backends.
74
+ """
75
+
76
+ # Table names from constants for consistent naming
77
+ TABLE_NAMES = POSTGRESQL_TABLE_NAMES
78
+
79
+ def __init__(
80
+ self,
81
+ host: str,
82
+ port: int,
83
+ database: str,
84
+ user: str,
85
+ password: str,
86
+ embedding_dim: int = 384,
87
+ pool_size: int = 10,
88
+ schema: str = "public",
89
+ ssl_mode: str = "prefer",
90
+ auto_migrate: bool = True,
91
+ ):
92
+ """
93
+ Initialize PostgreSQL storage.
94
+
95
+ Args:
96
+ host: Database host
97
+ port: Database port
98
+ database: Database name
99
+ user: Database user
100
+ password: Database password
101
+ embedding_dim: Dimension of embedding vectors
102
+ pool_size: Connection pool size
103
+ schema: Database schema (default: public)
104
+ ssl_mode: SSL mode (disable, allow, prefer, require, verify-ca, verify-full)
105
+ auto_migrate: If True, automatically apply pending migrations on startup
106
+ """
107
+ if not PSYCOPG_AVAILABLE:
108
+ raise ImportError(
109
+ "psycopg not installed. Install with: pip install 'alma-memory[postgres]'"
110
+ )
111
+
112
+ self.embedding_dim = embedding_dim
113
+ self.schema = schema
114
+ self._pgvector_available = False
115
+
116
+ # Migration support (lazy-loaded)
117
+ self._migration_runner = None
118
+ self._version_store = None
119
+
120
+ # Build connection string
121
+ conninfo = (
122
+ f"host={host} port={port} dbname={database} "
123
+ f"user={user} password={password} sslmode={ssl_mode}"
124
+ )
125
+
126
+ # Create connection pool
127
+ self._pool = ConnectionPool(
128
+ conninfo=conninfo,
129
+ min_size=1,
130
+ max_size=pool_size,
131
+ kwargs={"row_factory": dict_row},
132
+ )
133
+
134
+ # Initialize database
135
+ self._init_database()
136
+
137
+ # Auto-migrate if enabled
138
+ if auto_migrate:
139
+ self._ensure_migrated()
140
+
141
+ @classmethod
142
+ def from_config(cls, config: Dict[str, Any]) -> "PostgreSQLStorage":
143
+ """Create instance from configuration."""
144
+ pg_config = config.get("postgres", {})
145
+
146
+ # Support environment variable expansion
147
+ def get_value(key: str, default: Any = None) -> Any:
148
+ value = pg_config.get(key, default)
149
+ if (
150
+ isinstance(value, str)
151
+ and value.startswith("${")
152
+ and value.endswith("}")
153
+ ):
154
+ env_var = value[2:-1]
155
+ return os.environ.get(env_var, default)
156
+ return value
157
+
158
+ return cls(
159
+ host=get_value("host", "localhost"),
160
+ port=int(get_value("port", 5432)),
161
+ database=get_value("database", "alma_memory"),
162
+ user=get_value("user", "postgres"),
163
+ password=get_value("password", ""),
164
+ embedding_dim=int(config.get("embedding_dim", 384)),
165
+ pool_size=int(get_value("pool_size", 10)),
166
+ schema=get_value("schema", "public"),
167
+ ssl_mode=get_value("ssl_mode", "prefer"),
168
+ )
169
+
170
+ @contextmanager
171
+ def _get_connection(self):
172
+ """Get database connection from pool."""
173
+ with self._pool.connection() as conn:
174
+ yield conn
175
+
176
+ def _init_database(self):
177
+ """Initialize database schema and pgvector extension."""
178
+ with self._get_connection() as conn:
179
+ # Try to enable pgvector extension
180
+ try:
181
+ conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
182
+ conn.commit()
183
+ self._pgvector_available = True
184
+ logger.info("pgvector extension enabled")
185
+ except Exception as e:
186
+ conn.rollback() # Important: rollback to clear aborted transaction
187
+ logger.warning(f"pgvector not available: {e}. Using fallback search.")
188
+ self._pgvector_available = False
189
+
190
+ # Create tables
191
+ vector_type = (
192
+ f"VECTOR({self.embedding_dim})" if self._pgvector_available else "BYTEA"
193
+ )
194
+
195
+ # Heuristics table
196
+ heuristics_table = self.TABLE_NAMES[MemoryType.HEURISTICS]
197
+ conn.execute(f"""
198
+ CREATE TABLE IF NOT EXISTS {self.schema}.{heuristics_table} (
199
+ id TEXT PRIMARY KEY,
200
+ agent TEXT NOT NULL,
201
+ project_id TEXT NOT NULL,
202
+ condition TEXT NOT NULL,
203
+ strategy TEXT NOT NULL,
204
+ confidence REAL DEFAULT 0.0,
205
+ occurrence_count INTEGER DEFAULT 0,
206
+ success_count INTEGER DEFAULT 0,
207
+ last_validated TIMESTAMPTZ,
208
+ created_at TIMESTAMPTZ DEFAULT NOW(),
209
+ metadata JSONB,
210
+ embedding {vector_type}
211
+ )
212
+ """)
213
+ conn.execute(f"""
214
+ CREATE INDEX IF NOT EXISTS idx_heuristics_project_agent
215
+ ON {self.schema}.{heuristics_table}(project_id, agent)
216
+ """)
217
+ # Confidence index for efficient filtering by confidence score
218
+ conn.execute(f"""
219
+ CREATE INDEX IF NOT EXISTS idx_heuristics_confidence
220
+ ON {self.schema}.{heuristics_table}(project_id, confidence DESC)
221
+ """)
222
+
223
+ # Outcomes table
224
+ outcomes_table = self.TABLE_NAMES[MemoryType.OUTCOMES]
225
+ conn.execute(f"""
226
+ CREATE TABLE IF NOT EXISTS {self.schema}.{outcomes_table} (
227
+ id TEXT PRIMARY KEY,
228
+ agent TEXT NOT NULL,
229
+ project_id TEXT NOT NULL,
230
+ task_type TEXT,
231
+ task_description TEXT NOT NULL,
232
+ success BOOLEAN DEFAULT FALSE,
233
+ strategy_used TEXT,
234
+ duration_ms INTEGER,
235
+ error_message TEXT,
236
+ user_feedback TEXT,
237
+ timestamp TIMESTAMPTZ DEFAULT NOW(),
238
+ metadata JSONB,
239
+ embedding {vector_type}
240
+ )
241
+ """)
242
+ conn.execute(f"""
243
+ CREATE INDEX IF NOT EXISTS idx_outcomes_project_agent
244
+ ON {self.schema}.{outcomes_table}(project_id, agent)
245
+ """)
246
+ conn.execute(f"""
247
+ CREATE INDEX IF NOT EXISTS idx_outcomes_task_type
248
+ ON {self.schema}.{outcomes_table}(project_id, agent, task_type)
249
+ """)
250
+ conn.execute(f"""
251
+ CREATE INDEX IF NOT EXISTS idx_outcomes_timestamp
252
+ ON {self.schema}.{outcomes_table}(project_id, timestamp DESC)
253
+ """)
254
+
255
+ # User preferences table
256
+ preferences_table = self.TABLE_NAMES[MemoryType.PREFERENCES]
257
+ conn.execute(f"""
258
+ CREATE TABLE IF NOT EXISTS {self.schema}.{preferences_table} (
259
+ id TEXT PRIMARY KEY,
260
+ user_id TEXT NOT NULL,
261
+ category TEXT,
262
+ preference TEXT NOT NULL,
263
+ source TEXT,
264
+ confidence REAL DEFAULT 1.0,
265
+ timestamp TIMESTAMPTZ DEFAULT NOW(),
266
+ metadata JSONB
267
+ )
268
+ """)
269
+ conn.execute(f"""
270
+ CREATE INDEX IF NOT EXISTS idx_preferences_user
271
+ ON {self.schema}.{preferences_table}(user_id)
272
+ """)
273
+
274
+ # Domain knowledge table
275
+ domain_knowledge_table = self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]
276
+ conn.execute(f"""
277
+ CREATE TABLE IF NOT EXISTS {self.schema}.{domain_knowledge_table} (
278
+ id TEXT PRIMARY KEY,
279
+ agent TEXT NOT NULL,
280
+ project_id TEXT NOT NULL,
281
+ domain TEXT,
282
+ fact TEXT NOT NULL,
283
+ source TEXT,
284
+ confidence REAL DEFAULT 1.0,
285
+ last_verified TIMESTAMPTZ DEFAULT NOW(),
286
+ metadata JSONB,
287
+ embedding {vector_type}
288
+ )
289
+ """)
290
+ conn.execute(f"""
291
+ CREATE INDEX IF NOT EXISTS idx_domain_knowledge_project_agent
292
+ ON {self.schema}.{domain_knowledge_table}(project_id, agent)
293
+ """)
294
+ # Confidence index for efficient filtering by confidence score
295
+ conn.execute(f"""
296
+ CREATE INDEX IF NOT EXISTS idx_domain_knowledge_confidence
297
+ ON {self.schema}.{domain_knowledge_table}(project_id, confidence DESC)
298
+ """)
299
+
300
+ # Anti-patterns table
301
+ anti_patterns_table = self.TABLE_NAMES[MemoryType.ANTI_PATTERNS]
302
+ conn.execute(f"""
303
+ CREATE TABLE IF NOT EXISTS {self.schema}.{anti_patterns_table} (
304
+ id TEXT PRIMARY KEY,
305
+ agent TEXT NOT NULL,
306
+ project_id TEXT NOT NULL,
307
+ pattern TEXT NOT NULL,
308
+ why_bad TEXT,
309
+ better_alternative TEXT,
310
+ occurrence_count INTEGER DEFAULT 1,
311
+ last_seen TIMESTAMPTZ DEFAULT NOW(),
312
+ created_at TIMESTAMPTZ DEFAULT NOW(),
313
+ metadata JSONB,
314
+ embedding {vector_type}
315
+ )
316
+ """)
317
+ conn.execute(f"""
318
+ CREATE INDEX IF NOT EXISTS idx_anti_patterns_project_agent
319
+ ON {self.schema}.{anti_patterns_table}(project_id, agent)
320
+ """)
321
+
322
+ # Create vector indexes if pgvector available
323
+ # Using HNSW instead of IVFFlat because HNSW can be built on empty tables
324
+ # IVFFlat requires existing data to build, which causes silent failures on fresh databases
325
+ if self._pgvector_available:
326
+ # Vector-enabled tables use canonical memory type names
327
+ vector_tables = [
328
+ self.TABLE_NAMES[mt] for mt in MemoryType.VECTOR_ENABLED
329
+ ]
330
+ for table in vector_tables:
331
+ try:
332
+ conn.execute(f"""
333
+ CREATE INDEX IF NOT EXISTS idx_{table}_embedding
334
+ ON {self.schema}.{table}
335
+ USING hnsw (embedding vector_cosine_ops)
336
+ WITH (m = 16, ef_construction = 64)
337
+ """)
338
+ except Exception as e:
339
+ logger.warning(f"Failed to create HNSW index for {table}: {e}")
340
+
341
+ conn.commit()
342
+
343
+ def _embedding_to_db(self, embedding: Optional[List[float]]) -> Any:
344
+ """Convert embedding to database format."""
345
+ if embedding is None:
346
+ return None
347
+ if self._pgvector_available:
348
+ # pgvector expects string format: '[1.0, 2.0, 3.0]'
349
+ return f"[{','.join(str(x) for x in embedding)}]"
350
+ else:
351
+ # Store as bytes (requires numpy)
352
+ if not NUMPY_AVAILABLE:
353
+ raise ImportError("numpy required for non-pgvector embedding storage")
354
+ return np.array(embedding, dtype=np.float32).tobytes()
355
+
356
+ def _embedding_from_db(self, value: Any) -> Optional[List[float]]:
357
+ """Convert embedding from database format."""
358
+ if value is None:
359
+ return None
360
+ if self._pgvector_available:
361
+ # pgvector returns as string or list
362
+ if isinstance(value, str):
363
+ value = value.strip("[]")
364
+ return [float(x) for x in value.split(",")]
365
+ return list(value)
366
+ else:
367
+ # Stored as bytes (requires numpy)
368
+ if not NUMPY_AVAILABLE or np is None:
369
+ return None
370
+ return np.frombuffer(value, dtype=np.float32).tolist()
371
+
372
+ def _cosine_similarity(self, a: List[float], b: List[float]) -> float:
373
+ """Compute cosine similarity between two vectors."""
374
+ if not NUMPY_AVAILABLE or np is None:
375
+ # Fallback to pure Python
376
+ dot = sum(x * y for x, y in zip(a, b, strict=False))
377
+ norm_a = sum(x * x for x in a) ** 0.5
378
+ norm_b = sum(x * x for x in b) ** 0.5
379
+ return dot / (norm_a * norm_b) if norm_a and norm_b else 0.0
380
+ a_arr = np.array(a)
381
+ b_arr = np.array(b)
382
+ return float(
383
+ np.dot(a_arr, b_arr) / (np.linalg.norm(a_arr) * np.linalg.norm(b_arr))
384
+ )
385
+
386
+ # ==================== WRITE OPERATIONS ====================
387
+
388
+ def save_heuristic(self, heuristic: Heuristic) -> str:
389
+ """Save a heuristic."""
390
+ with self._get_connection() as conn:
391
+ conn.execute(
392
+ f"""
393
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
394
+ (id, agent, project_id, condition, strategy, confidence,
395
+ occurrence_count, success_count, last_validated, created_at, metadata, embedding)
396
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
397
+ ON CONFLICT (id) DO UPDATE SET
398
+ condition = EXCLUDED.condition,
399
+ strategy = EXCLUDED.strategy,
400
+ confidence = EXCLUDED.confidence,
401
+ occurrence_count = EXCLUDED.occurrence_count,
402
+ success_count = EXCLUDED.success_count,
403
+ last_validated = EXCLUDED.last_validated,
404
+ metadata = EXCLUDED.metadata,
405
+ embedding = EXCLUDED.embedding
406
+ """,
407
+ (
408
+ heuristic.id,
409
+ heuristic.agent,
410
+ heuristic.project_id,
411
+ heuristic.condition,
412
+ heuristic.strategy,
413
+ heuristic.confidence,
414
+ heuristic.occurrence_count,
415
+ heuristic.success_count,
416
+ heuristic.last_validated,
417
+ heuristic.created_at,
418
+ json.dumps(heuristic.metadata) if heuristic.metadata else None,
419
+ self._embedding_to_db(heuristic.embedding),
420
+ ),
421
+ )
422
+ conn.commit()
423
+
424
+ logger.debug(f"Saved heuristic: {heuristic.id}")
425
+ return heuristic.id
426
+
427
+ def save_outcome(self, outcome: Outcome) -> str:
428
+ """Save an outcome."""
429
+ with self._get_connection() as conn:
430
+ conn.execute(
431
+ f"""
432
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]}
433
+ (id, agent, project_id, task_type, task_description, success,
434
+ strategy_used, duration_ms, error_message, user_feedback, timestamp, metadata, embedding)
435
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
436
+ ON CONFLICT (id) DO UPDATE SET
437
+ task_description = EXCLUDED.task_description,
438
+ success = EXCLUDED.success,
439
+ strategy_used = EXCLUDED.strategy_used,
440
+ duration_ms = EXCLUDED.duration_ms,
441
+ error_message = EXCLUDED.error_message,
442
+ user_feedback = EXCLUDED.user_feedback,
443
+ metadata = EXCLUDED.metadata,
444
+ embedding = EXCLUDED.embedding
445
+ """,
446
+ (
447
+ outcome.id,
448
+ outcome.agent,
449
+ outcome.project_id,
450
+ outcome.task_type,
451
+ outcome.task_description,
452
+ outcome.success,
453
+ outcome.strategy_used,
454
+ outcome.duration_ms,
455
+ outcome.error_message,
456
+ outcome.user_feedback,
457
+ outcome.timestamp,
458
+ json.dumps(outcome.metadata) if outcome.metadata else None,
459
+ self._embedding_to_db(outcome.embedding),
460
+ ),
461
+ )
462
+ conn.commit()
463
+
464
+ logger.debug(f"Saved outcome: {outcome.id}")
465
+ return outcome.id
466
+
467
+ def save_user_preference(self, preference: UserPreference) -> str:
468
+ """Save a user preference."""
469
+ with self._get_connection() as conn:
470
+ conn.execute(
471
+ f"""
472
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.PREFERENCES]}
473
+ (id, user_id, category, preference, source, confidence, timestamp, metadata)
474
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
475
+ ON CONFLICT (id) DO UPDATE SET
476
+ preference = EXCLUDED.preference,
477
+ source = EXCLUDED.source,
478
+ confidence = EXCLUDED.confidence,
479
+ metadata = EXCLUDED.metadata
480
+ """,
481
+ (
482
+ preference.id,
483
+ preference.user_id,
484
+ preference.category,
485
+ preference.preference,
486
+ preference.source,
487
+ preference.confidence,
488
+ preference.timestamp,
489
+ json.dumps(preference.metadata) if preference.metadata else None,
490
+ ),
491
+ )
492
+ conn.commit()
493
+
494
+ logger.debug(f"Saved preference: {preference.id}")
495
+ return preference.id
496
+
497
+ def save_domain_knowledge(self, knowledge: DomainKnowledge) -> str:
498
+ """Save domain knowledge."""
499
+ with self._get_connection() as conn:
500
+ conn.execute(
501
+ f"""
502
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]}
503
+ (id, agent, project_id, domain, fact, source, confidence, last_verified, metadata, embedding)
504
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
505
+ ON CONFLICT (id) DO UPDATE SET
506
+ fact = EXCLUDED.fact,
507
+ source = EXCLUDED.source,
508
+ confidence = EXCLUDED.confidence,
509
+ last_verified = EXCLUDED.last_verified,
510
+ metadata = EXCLUDED.metadata,
511
+ embedding = EXCLUDED.embedding
512
+ """,
513
+ (
514
+ knowledge.id,
515
+ knowledge.agent,
516
+ knowledge.project_id,
517
+ knowledge.domain,
518
+ knowledge.fact,
519
+ knowledge.source,
520
+ knowledge.confidence,
521
+ knowledge.last_verified,
522
+ json.dumps(knowledge.metadata) if knowledge.metadata else None,
523
+ self._embedding_to_db(knowledge.embedding),
524
+ ),
525
+ )
526
+ conn.commit()
527
+
528
+ logger.debug(f"Saved domain knowledge: {knowledge.id}")
529
+ return knowledge.id
530
+
531
+ def save_anti_pattern(self, anti_pattern: AntiPattern) -> str:
532
+ """Save an anti-pattern."""
533
+ with self._get_connection() as conn:
534
+ conn.execute(
535
+ f"""
536
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.ANTI_PATTERNS]}
537
+ (id, agent, project_id, pattern, why_bad, better_alternative,
538
+ occurrence_count, last_seen, created_at, metadata, embedding)
539
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
540
+ ON CONFLICT (id) DO UPDATE SET
541
+ pattern = EXCLUDED.pattern,
542
+ why_bad = EXCLUDED.why_bad,
543
+ better_alternative = EXCLUDED.better_alternative,
544
+ occurrence_count = EXCLUDED.occurrence_count,
545
+ last_seen = EXCLUDED.last_seen,
546
+ metadata = EXCLUDED.metadata,
547
+ embedding = EXCLUDED.embedding
548
+ """,
549
+ (
550
+ anti_pattern.id,
551
+ anti_pattern.agent,
552
+ anti_pattern.project_id,
553
+ anti_pattern.pattern,
554
+ anti_pattern.why_bad,
555
+ anti_pattern.better_alternative,
556
+ anti_pattern.occurrence_count,
557
+ anti_pattern.last_seen,
558
+ anti_pattern.created_at,
559
+ (
560
+ json.dumps(anti_pattern.metadata)
561
+ if anti_pattern.metadata
562
+ else None
563
+ ),
564
+ self._embedding_to_db(anti_pattern.embedding),
565
+ ),
566
+ )
567
+ conn.commit()
568
+
569
+ logger.debug(f"Saved anti-pattern: {anti_pattern.id}")
570
+ return anti_pattern.id
571
+
572
+ # ==================== BATCH WRITE OPERATIONS ====================
573
+
574
+ def save_heuristics(self, heuristics: List[Heuristic]) -> List[str]:
575
+ """Save multiple heuristics in a batch using executemany."""
576
+ if not heuristics:
577
+ return []
578
+
579
+ with self._get_connection() as conn:
580
+ conn.executemany(
581
+ f"""
582
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
583
+ (id, agent, project_id, condition, strategy, confidence,
584
+ occurrence_count, success_count, last_validated, created_at, metadata, embedding)
585
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
586
+ ON CONFLICT (id) DO UPDATE SET
587
+ condition = EXCLUDED.condition,
588
+ strategy = EXCLUDED.strategy,
589
+ confidence = EXCLUDED.confidence,
590
+ occurrence_count = EXCLUDED.occurrence_count,
591
+ success_count = EXCLUDED.success_count,
592
+ last_validated = EXCLUDED.last_validated,
593
+ metadata = EXCLUDED.metadata,
594
+ embedding = EXCLUDED.embedding
595
+ """,
596
+ [
597
+ (
598
+ h.id,
599
+ h.agent,
600
+ h.project_id,
601
+ h.condition,
602
+ h.strategy,
603
+ h.confidence,
604
+ h.occurrence_count,
605
+ h.success_count,
606
+ h.last_validated,
607
+ h.created_at,
608
+ json.dumps(h.metadata) if h.metadata else None,
609
+ self._embedding_to_db(h.embedding),
610
+ )
611
+ for h in heuristics
612
+ ],
613
+ )
614
+ conn.commit()
615
+
616
+ logger.debug(f"Batch saved {len(heuristics)} heuristics")
617
+ return [h.id for h in heuristics]
618
+
619
+ def save_outcomes(self, outcomes: List[Outcome]) -> List[str]:
620
+ """Save multiple outcomes in a batch using executemany."""
621
+ if not outcomes:
622
+ return []
623
+
624
+ with self._get_connection() as conn:
625
+ conn.executemany(
626
+ f"""
627
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]}
628
+ (id, agent, project_id, task_type, task_description, success,
629
+ strategy_used, duration_ms, error_message, user_feedback, timestamp, metadata, embedding)
630
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
631
+ ON CONFLICT (id) DO UPDATE SET
632
+ task_description = EXCLUDED.task_description,
633
+ success = EXCLUDED.success,
634
+ strategy_used = EXCLUDED.strategy_used,
635
+ duration_ms = EXCLUDED.duration_ms,
636
+ error_message = EXCLUDED.error_message,
637
+ user_feedback = EXCLUDED.user_feedback,
638
+ metadata = EXCLUDED.metadata,
639
+ embedding = EXCLUDED.embedding
640
+ """,
641
+ [
642
+ (
643
+ o.id,
644
+ o.agent,
645
+ o.project_id,
646
+ o.task_type,
647
+ o.task_description,
648
+ o.success,
649
+ o.strategy_used,
650
+ o.duration_ms,
651
+ o.error_message,
652
+ o.user_feedback,
653
+ o.timestamp,
654
+ json.dumps(o.metadata) if o.metadata else None,
655
+ self._embedding_to_db(o.embedding),
656
+ )
657
+ for o in outcomes
658
+ ],
659
+ )
660
+ conn.commit()
661
+
662
+ logger.debug(f"Batch saved {len(outcomes)} outcomes")
663
+ return [o.id for o in outcomes]
664
+
665
+ def save_domain_knowledge_batch(
666
+ self, knowledge_items: List[DomainKnowledge]
667
+ ) -> List[str]:
668
+ """Save multiple domain knowledge items in a batch using executemany."""
669
+ if not knowledge_items:
670
+ return []
671
+
672
+ with self._get_connection() as conn:
673
+ conn.executemany(
674
+ f"""
675
+ INSERT INTO {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]}
676
+ (id, agent, project_id, domain, fact, source, confidence, last_verified, metadata, embedding)
677
+ VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
678
+ ON CONFLICT (id) DO UPDATE SET
679
+ fact = EXCLUDED.fact,
680
+ source = EXCLUDED.source,
681
+ confidence = EXCLUDED.confidence,
682
+ last_verified = EXCLUDED.last_verified,
683
+ metadata = EXCLUDED.metadata,
684
+ embedding = EXCLUDED.embedding
685
+ """,
686
+ [
687
+ (
688
+ k.id,
689
+ k.agent,
690
+ k.project_id,
691
+ k.domain,
692
+ k.fact,
693
+ k.source,
694
+ k.confidence,
695
+ k.last_verified,
696
+ json.dumps(k.metadata) if k.metadata else None,
697
+ self._embedding_to_db(k.embedding),
698
+ )
699
+ for k in knowledge_items
700
+ ],
701
+ )
702
+ conn.commit()
703
+
704
+ logger.debug(f"Batch saved {len(knowledge_items)} domain knowledge items")
705
+ return [k.id for k in knowledge_items]
706
+
707
+ # ==================== READ OPERATIONS ====================
708
+
709
+ def get_heuristics(
710
+ self,
711
+ project_id: str,
712
+ agent: Optional[str] = None,
713
+ embedding: Optional[List[float]] = None,
714
+ top_k: int = 5,
715
+ min_confidence: float = 0.0,
716
+ ) -> List[Heuristic]:
717
+ """Get heuristics with optional vector search."""
718
+ with self._get_connection() as conn:
719
+ if embedding and self._pgvector_available:
720
+ # Use pgvector similarity search
721
+ query = f"""
722
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
723
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
724
+ WHERE project_id = %s AND confidence >= %s
725
+ """
726
+ params: List[Any] = [
727
+ self._embedding_to_db(embedding),
728
+ project_id,
729
+ min_confidence,
730
+ ]
731
+
732
+ if agent:
733
+ query += " AND agent = %s"
734
+ params.append(agent)
735
+
736
+ query += " ORDER BY similarity DESC LIMIT %s"
737
+ params.append(top_k)
738
+ else:
739
+ # Standard query
740
+ query = f"""
741
+ SELECT *
742
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
743
+ WHERE project_id = %s AND confidence >= %s
744
+ """
745
+ params = [project_id, min_confidence]
746
+
747
+ if agent:
748
+ query += " AND agent = %s"
749
+ params.append(agent)
750
+
751
+ query += " ORDER BY confidence DESC LIMIT %s"
752
+ params.append(top_k)
753
+
754
+ cursor = conn.execute(query, params)
755
+ rows = cursor.fetchall()
756
+
757
+ results = [self._row_to_heuristic(row) for row in rows]
758
+
759
+ # If embedding provided but pgvector not available, do app-level filtering
760
+ if embedding and not self._pgvector_available and results:
761
+ results = self._filter_by_similarity(results, embedding, top_k, "embedding")
762
+
763
+ return results
764
+
765
+ def get_outcomes(
766
+ self,
767
+ project_id: str,
768
+ agent: Optional[str] = None,
769
+ task_type: Optional[str] = None,
770
+ embedding: Optional[List[float]] = None,
771
+ top_k: int = 5,
772
+ success_only: bool = False,
773
+ ) -> List[Outcome]:
774
+ """Get outcomes with optional vector search."""
775
+ with self._get_connection() as conn:
776
+ if embedding and self._pgvector_available:
777
+ query = f"""
778
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
779
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]}
780
+ WHERE project_id = %s
781
+ """
782
+ params: List[Any] = [self._embedding_to_db(embedding), project_id]
783
+ else:
784
+ query = f"""
785
+ SELECT *
786
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]}
787
+ WHERE project_id = %s
788
+ """
789
+ params = [project_id]
790
+
791
+ if agent:
792
+ query += " AND agent = %s"
793
+ params.append(agent)
794
+
795
+ if task_type:
796
+ query += " AND task_type = %s"
797
+ params.append(task_type)
798
+
799
+ if success_only:
800
+ query += " AND success = TRUE"
801
+
802
+ if embedding and self._pgvector_available:
803
+ query += " ORDER BY similarity DESC LIMIT %s"
804
+ else:
805
+ query += " ORDER BY timestamp DESC LIMIT %s"
806
+ params.append(top_k)
807
+
808
+ cursor = conn.execute(query, params)
809
+ rows = cursor.fetchall()
810
+
811
+ results = [self._row_to_outcome(row) for row in rows]
812
+
813
+ if embedding and not self._pgvector_available and results:
814
+ results = self._filter_by_similarity(results, embedding, top_k, "embedding")
815
+
816
+ return results
817
+
818
+ def get_user_preferences(
819
+ self,
820
+ user_id: str,
821
+ category: Optional[str] = None,
822
+ ) -> List[UserPreference]:
823
+ """Get user preferences."""
824
+ with self._get_connection() as conn:
825
+ query = f"SELECT * FROM {self.schema}.{self.TABLE_NAMES[MemoryType.PREFERENCES]} WHERE user_id = %s"
826
+ params: List[Any] = [user_id]
827
+
828
+ if category:
829
+ query += " AND category = %s"
830
+ params.append(category)
831
+
832
+ cursor = conn.execute(query, params)
833
+ rows = cursor.fetchall()
834
+
835
+ return [self._row_to_preference(row) for row in rows]
836
+
837
+ def get_domain_knowledge(
838
+ self,
839
+ project_id: str,
840
+ agent: Optional[str] = None,
841
+ domain: Optional[str] = None,
842
+ embedding: Optional[List[float]] = None,
843
+ top_k: int = 5,
844
+ ) -> List[DomainKnowledge]:
845
+ """Get domain knowledge with optional vector search."""
846
+ with self._get_connection() as conn:
847
+ if embedding and self._pgvector_available:
848
+ query = f"""
849
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
850
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]}
851
+ WHERE project_id = %s
852
+ """
853
+ params: List[Any] = [self._embedding_to_db(embedding), project_id]
854
+ else:
855
+ query = f"""
856
+ SELECT *
857
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]}
858
+ WHERE project_id = %s
859
+ """
860
+ params = [project_id]
861
+
862
+ if agent:
863
+ query += " AND agent = %s"
864
+ params.append(agent)
865
+
866
+ if domain:
867
+ query += " AND domain = %s"
868
+ params.append(domain)
869
+
870
+ if embedding and self._pgvector_available:
871
+ query += " ORDER BY similarity DESC LIMIT %s"
872
+ else:
873
+ query += " ORDER BY confidence DESC LIMIT %s"
874
+ params.append(top_k)
875
+
876
+ cursor = conn.execute(query, params)
877
+ rows = cursor.fetchall()
878
+
879
+ results = [self._row_to_domain_knowledge(row) for row in rows]
880
+
881
+ if embedding and not self._pgvector_available and results:
882
+ results = self._filter_by_similarity(results, embedding, top_k, "embedding")
883
+
884
+ return results
885
+
886
+ def get_anti_patterns(
887
+ self,
888
+ project_id: str,
889
+ agent: Optional[str] = None,
890
+ embedding: Optional[List[float]] = None,
891
+ top_k: int = 5,
892
+ ) -> List[AntiPattern]:
893
+ """Get anti-patterns with optional vector search."""
894
+ with self._get_connection() as conn:
895
+ if embedding and self._pgvector_available:
896
+ query = f"""
897
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
898
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.ANTI_PATTERNS]}
899
+ WHERE project_id = %s
900
+ """
901
+ params: List[Any] = [self._embedding_to_db(embedding), project_id]
902
+ else:
903
+ query = f"""
904
+ SELECT *
905
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.ANTI_PATTERNS]}
906
+ WHERE project_id = %s
907
+ """
908
+ params = [project_id]
909
+
910
+ if agent:
911
+ query += " AND agent = %s"
912
+ params.append(agent)
913
+
914
+ if embedding and self._pgvector_available:
915
+ query += " ORDER BY similarity DESC LIMIT %s"
916
+ else:
917
+ query += " ORDER BY occurrence_count DESC LIMIT %s"
918
+ params.append(top_k)
919
+
920
+ cursor = conn.execute(query, params)
921
+ rows = cursor.fetchall()
922
+
923
+ results = [self._row_to_anti_pattern(row) for row in rows]
924
+
925
+ if embedding and not self._pgvector_available and results:
926
+ results = self._filter_by_similarity(results, embedding, top_k, "embedding")
927
+
928
+ return results
929
+
930
+ def _filter_by_similarity(
931
+ self,
932
+ items: List[Any],
933
+ query_embedding: List[float],
934
+ top_k: int,
935
+ embedding_attr: str,
936
+ ) -> List[Any]:
937
+ """Filter items by cosine similarity (fallback when pgvector unavailable)."""
938
+ scored = []
939
+ for item in items:
940
+ item_embedding = getattr(item, embedding_attr, None)
941
+ if item_embedding:
942
+ similarity = self._cosine_similarity(query_embedding, item_embedding)
943
+ scored.append((item, similarity))
944
+ else:
945
+ scored.append((item, 0.0))
946
+
947
+ scored.sort(key=lambda x: x[1], reverse=True)
948
+ return [item for item, _ in scored[:top_k]]
949
+
950
+ # ==================== MULTI-AGENT MEMORY SHARING ====================
951
+
952
+ def get_heuristics_for_agents(
953
+ self,
954
+ project_id: str,
955
+ agents: List[str],
956
+ embedding: Optional[List[float]] = None,
957
+ top_k: int = 5,
958
+ min_confidence: float = 0.0,
959
+ ) -> List[Heuristic]:
960
+ """Get heuristics from multiple agents using optimized ANY query."""
961
+ if not agents:
962
+ return []
963
+
964
+ with self._get_connection() as conn:
965
+ if embedding and self._pgvector_available:
966
+ query = f"""
967
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
968
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
969
+ WHERE project_id = %s AND confidence >= %s AND agent = ANY(%s)
970
+ ORDER BY similarity DESC LIMIT %s
971
+ """
972
+ params: List[Any] = [
973
+ self._embedding_to_db(embedding),
974
+ project_id,
975
+ min_confidence,
976
+ agents,
977
+ top_k * len(agents),
978
+ ]
979
+ else:
980
+ query = f"""
981
+ SELECT *
982
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
983
+ WHERE project_id = %s AND confidence >= %s AND agent = ANY(%s)
984
+ ORDER BY confidence DESC LIMIT %s
985
+ """
986
+ params = [project_id, min_confidence, agents, top_k * len(agents)]
987
+
988
+ cursor = conn.execute(query, params)
989
+ rows = cursor.fetchall()
990
+
991
+ results = [self._row_to_heuristic(row) for row in rows]
992
+
993
+ if embedding and not self._pgvector_available and results:
994
+ results = self._filter_by_similarity(
995
+ results, embedding, top_k * len(agents), "embedding"
996
+ )
997
+
998
+ return results
999
+
1000
+ def get_outcomes_for_agents(
1001
+ self,
1002
+ project_id: str,
1003
+ agents: List[str],
1004
+ task_type: Optional[str] = None,
1005
+ embedding: Optional[List[float]] = None,
1006
+ top_k: int = 5,
1007
+ success_only: bool = False,
1008
+ ) -> List[Outcome]:
1009
+ """Get outcomes from multiple agents using optimized ANY query."""
1010
+ if not agents:
1011
+ return []
1012
+
1013
+ with self._get_connection() as conn:
1014
+ if embedding and self._pgvector_available:
1015
+ query = f"""
1016
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
1017
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]}
1018
+ WHERE project_id = %s AND agent = ANY(%s)
1019
+ """
1020
+ params: List[Any] = [
1021
+ self._embedding_to_db(embedding),
1022
+ project_id,
1023
+ agents,
1024
+ ]
1025
+ else:
1026
+ query = f"""
1027
+ SELECT *
1028
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]}
1029
+ WHERE project_id = %s AND agent = ANY(%s)
1030
+ """
1031
+ params = [project_id, agents]
1032
+
1033
+ if task_type:
1034
+ query += " AND task_type = %s"
1035
+ params.append(task_type)
1036
+
1037
+ if success_only:
1038
+ query += " AND success = TRUE"
1039
+
1040
+ if embedding and self._pgvector_available:
1041
+ query += " ORDER BY similarity DESC LIMIT %s"
1042
+ else:
1043
+ query += " ORDER BY timestamp DESC LIMIT %s"
1044
+ params.append(top_k * len(agents))
1045
+
1046
+ cursor = conn.execute(query, params)
1047
+ rows = cursor.fetchall()
1048
+
1049
+ results = [self._row_to_outcome(row) for row in rows]
1050
+
1051
+ if embedding and not self._pgvector_available and results:
1052
+ results = self._filter_by_similarity(
1053
+ results, embedding, top_k * len(agents), "embedding"
1054
+ )
1055
+
1056
+ return results
1057
+
1058
+ def get_domain_knowledge_for_agents(
1059
+ self,
1060
+ project_id: str,
1061
+ agents: List[str],
1062
+ domain: Optional[str] = None,
1063
+ embedding: Optional[List[float]] = None,
1064
+ top_k: int = 5,
1065
+ ) -> List[DomainKnowledge]:
1066
+ """Get domain knowledge from multiple agents using optimized ANY query."""
1067
+ if not agents:
1068
+ return []
1069
+
1070
+ with self._get_connection() as conn:
1071
+ if embedding and self._pgvector_available:
1072
+ query = f"""
1073
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
1074
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]}
1075
+ WHERE project_id = %s AND agent = ANY(%s)
1076
+ """
1077
+ params: List[Any] = [
1078
+ self._embedding_to_db(embedding),
1079
+ project_id,
1080
+ agents,
1081
+ ]
1082
+ else:
1083
+ query = f"""
1084
+ SELECT *
1085
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]}
1086
+ WHERE project_id = %s AND agent = ANY(%s)
1087
+ """
1088
+ params = [project_id, agents]
1089
+
1090
+ if domain:
1091
+ query += " AND domain = %s"
1092
+ params.append(domain)
1093
+
1094
+ if embedding and self._pgvector_available:
1095
+ query += " ORDER BY similarity DESC LIMIT %s"
1096
+ else:
1097
+ query += " ORDER BY confidence DESC LIMIT %s"
1098
+ params.append(top_k * len(agents))
1099
+
1100
+ cursor = conn.execute(query, params)
1101
+ rows = cursor.fetchall()
1102
+
1103
+ results = [self._row_to_domain_knowledge(row) for row in rows]
1104
+
1105
+ if embedding and not self._pgvector_available and results:
1106
+ results = self._filter_by_similarity(
1107
+ results, embedding, top_k * len(agents), "embedding"
1108
+ )
1109
+
1110
+ return results
1111
+
1112
+ def get_anti_patterns_for_agents(
1113
+ self,
1114
+ project_id: str,
1115
+ agents: List[str],
1116
+ embedding: Optional[List[float]] = None,
1117
+ top_k: int = 5,
1118
+ ) -> List[AntiPattern]:
1119
+ """Get anti-patterns from multiple agents using optimized ANY query."""
1120
+ if not agents:
1121
+ return []
1122
+
1123
+ with self._get_connection() as conn:
1124
+ if embedding and self._pgvector_available:
1125
+ query = f"""
1126
+ SELECT *, 1 - (embedding <=> %s::vector) as similarity
1127
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.ANTI_PATTERNS]}
1128
+ WHERE project_id = %s AND agent = ANY(%s)
1129
+ """
1130
+ params: List[Any] = [
1131
+ self._embedding_to_db(embedding),
1132
+ project_id,
1133
+ agents,
1134
+ ]
1135
+ else:
1136
+ query = f"""
1137
+ SELECT *
1138
+ FROM {self.schema}.{self.TABLE_NAMES[MemoryType.ANTI_PATTERNS]}
1139
+ WHERE project_id = %s AND agent = ANY(%s)
1140
+ """
1141
+ params = [project_id, agents]
1142
+
1143
+ if embedding and self._pgvector_available:
1144
+ query += " ORDER BY similarity DESC LIMIT %s"
1145
+ else:
1146
+ query += " ORDER BY occurrence_count DESC LIMIT %s"
1147
+ params.append(top_k * len(agents))
1148
+
1149
+ cursor = conn.execute(query, params)
1150
+ rows = cursor.fetchall()
1151
+
1152
+ results = [self._row_to_anti_pattern(row) for row in rows]
1153
+
1154
+ if embedding and not self._pgvector_available and results:
1155
+ results = self._filter_by_similarity(
1156
+ results, embedding, top_k * len(agents), "embedding"
1157
+ )
1158
+
1159
+ return results
1160
+
1161
+ # ==================== UPDATE OPERATIONS ====================
1162
+
1163
+ def update_heuristic(
1164
+ self,
1165
+ heuristic_id: str,
1166
+ updates: Dict[str, Any],
1167
+ ) -> bool:
1168
+ """Update a heuristic's fields."""
1169
+ if not updates:
1170
+ return False
1171
+
1172
+ set_clauses = []
1173
+ params = []
1174
+ for key, value in updates.items():
1175
+ if key == "metadata" and value:
1176
+ value = json.dumps(value)
1177
+ set_clauses.append(f"{key} = %s")
1178
+ params.append(value)
1179
+
1180
+ params.append(heuristic_id)
1181
+
1182
+ with self._get_connection() as conn:
1183
+ cursor = conn.execute(
1184
+ f"UPDATE {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]} SET {', '.join(set_clauses)} WHERE id = %s",
1185
+ params,
1186
+ )
1187
+ conn.commit()
1188
+ return cursor.rowcount > 0
1189
+
1190
+ def increment_heuristic_occurrence(
1191
+ self,
1192
+ heuristic_id: str,
1193
+ success: bool,
1194
+ ) -> bool:
1195
+ """Increment heuristic occurrence count."""
1196
+ with self._get_connection() as conn:
1197
+ if success:
1198
+ cursor = conn.execute(
1199
+ f"""
1200
+ UPDATE {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
1201
+ SET occurrence_count = occurrence_count + 1,
1202
+ success_count = success_count + 1,
1203
+ last_validated = %s
1204
+ WHERE id = %s
1205
+ """,
1206
+ (datetime.now(timezone.utc), heuristic_id),
1207
+ )
1208
+ else:
1209
+ cursor = conn.execute(
1210
+ f"""
1211
+ UPDATE {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]}
1212
+ SET occurrence_count = occurrence_count + 1,
1213
+ last_validated = %s
1214
+ WHERE id = %s
1215
+ """,
1216
+ (datetime.now(timezone.utc), heuristic_id),
1217
+ )
1218
+ conn.commit()
1219
+ return cursor.rowcount > 0
1220
+
1221
+ def update_heuristic_confidence(
1222
+ self,
1223
+ heuristic_id: str,
1224
+ new_confidence: float,
1225
+ ) -> bool:
1226
+ """Update confidence score for a heuristic."""
1227
+ with self._get_connection() as conn:
1228
+ cursor = conn.execute(
1229
+ f"UPDATE {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]} SET confidence = %s WHERE id = %s",
1230
+ (new_confidence, heuristic_id),
1231
+ )
1232
+ conn.commit()
1233
+ return cursor.rowcount > 0
1234
+
1235
+ def update_knowledge_confidence(
1236
+ self,
1237
+ knowledge_id: str,
1238
+ new_confidence: float,
1239
+ ) -> bool:
1240
+ """Update confidence score for domain knowledge."""
1241
+ with self._get_connection() as conn:
1242
+ cursor = conn.execute(
1243
+ f"UPDATE {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]} SET confidence = %s WHERE id = %s",
1244
+ (new_confidence, knowledge_id),
1245
+ )
1246
+ conn.commit()
1247
+ return cursor.rowcount > 0
1248
+
1249
+ # ==================== DELETE OPERATIONS ====================
1250
+
1251
+ def delete_heuristic(self, heuristic_id: str) -> bool:
1252
+ """Delete a heuristic by ID."""
1253
+ with self._get_connection() as conn:
1254
+ cursor = conn.execute(
1255
+ f"DELETE FROM {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]} WHERE id = %s",
1256
+ (heuristic_id,),
1257
+ )
1258
+ conn.commit()
1259
+ return cursor.rowcount > 0
1260
+
1261
+ def delete_outcome(self, outcome_id: str) -> bool:
1262
+ """Delete an outcome by ID."""
1263
+ with self._get_connection() as conn:
1264
+ cursor = conn.execute(
1265
+ f"DELETE FROM {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]} WHERE id = %s",
1266
+ (outcome_id,),
1267
+ )
1268
+ conn.commit()
1269
+ return cursor.rowcount > 0
1270
+
1271
+ def delete_domain_knowledge(self, knowledge_id: str) -> bool:
1272
+ """Delete domain knowledge by ID."""
1273
+ with self._get_connection() as conn:
1274
+ cursor = conn.execute(
1275
+ f"DELETE FROM {self.schema}.{self.TABLE_NAMES[MemoryType.DOMAIN_KNOWLEDGE]} WHERE id = %s",
1276
+ (knowledge_id,),
1277
+ )
1278
+ conn.commit()
1279
+ return cursor.rowcount > 0
1280
+
1281
+ def delete_anti_pattern(self, anti_pattern_id: str) -> bool:
1282
+ """Delete an anti-pattern by ID."""
1283
+ with self._get_connection() as conn:
1284
+ cursor = conn.execute(
1285
+ f"DELETE FROM {self.schema}.{self.TABLE_NAMES[MemoryType.ANTI_PATTERNS]} WHERE id = %s",
1286
+ (anti_pattern_id,),
1287
+ )
1288
+ conn.commit()
1289
+ return cursor.rowcount > 0
1290
+
1291
+ def delete_outcomes_older_than(
1292
+ self,
1293
+ project_id: str,
1294
+ older_than: datetime,
1295
+ agent: Optional[str] = None,
1296
+ ) -> int:
1297
+ """Delete old outcomes."""
1298
+ with self._get_connection() as conn:
1299
+ query = f"DELETE FROM {self.schema}.{self.TABLE_NAMES[MemoryType.OUTCOMES]} WHERE project_id = %s AND timestamp < %s"
1300
+ params: List[Any] = [project_id, older_than]
1301
+
1302
+ if agent:
1303
+ query += " AND agent = %s"
1304
+ params.append(agent)
1305
+
1306
+ cursor = conn.execute(query, params)
1307
+ conn.commit()
1308
+ deleted = cursor.rowcount
1309
+
1310
+ logger.info(f"Deleted {deleted} old outcomes")
1311
+ return deleted
1312
+
1313
+ def delete_low_confidence_heuristics(
1314
+ self,
1315
+ project_id: str,
1316
+ below_confidence: float,
1317
+ agent: Optional[str] = None,
1318
+ ) -> int:
1319
+ """Delete low-confidence heuristics."""
1320
+ with self._get_connection() as conn:
1321
+ query = f"DELETE FROM {self.schema}.{self.TABLE_NAMES[MemoryType.HEURISTICS]} WHERE project_id = %s AND confidence < %s"
1322
+ params: List[Any] = [project_id, below_confidence]
1323
+
1324
+ if agent:
1325
+ query += " AND agent = %s"
1326
+ params.append(agent)
1327
+
1328
+ cursor = conn.execute(query, params)
1329
+ conn.commit()
1330
+ deleted = cursor.rowcount
1331
+
1332
+ logger.info(f"Deleted {deleted} low-confidence heuristics")
1333
+ return deleted
1334
+
1335
+ # ==================== STATS ====================
1336
+
1337
+ def get_stats(
1338
+ self,
1339
+ project_id: str,
1340
+ agent: Optional[str] = None,
1341
+ ) -> Dict[str, Any]:
1342
+ """Get memory statistics."""
1343
+ stats = {
1344
+ "project_id": project_id,
1345
+ "agent": agent,
1346
+ "storage_type": "postgresql",
1347
+ "pgvector_available": self._pgvector_available,
1348
+ }
1349
+
1350
+ with self._get_connection() as conn:
1351
+ # Use canonical memory types for stats
1352
+ for memory_type in MemoryType.ALL:
1353
+ table = self.TABLE_NAMES[memory_type]
1354
+ if memory_type == MemoryType.PREFERENCES:
1355
+ # Preferences don't have project_id
1356
+ cursor = conn.execute(
1357
+ f"SELECT COUNT(*) as count FROM {self.schema}.{table}"
1358
+ )
1359
+ row = cursor.fetchone()
1360
+ stats[f"{memory_type}_count"] = row["count"] if row else 0
1361
+ else:
1362
+ query = f"SELECT COUNT(*) as count FROM {self.schema}.{table} WHERE project_id = %s"
1363
+ params: List[Any] = [project_id]
1364
+ if agent:
1365
+ query += " AND agent = %s"
1366
+ params.append(agent)
1367
+ cursor = conn.execute(query, params)
1368
+ row = cursor.fetchone()
1369
+ stats[f"{memory_type}_count"] = row["count"] if row else 0
1370
+
1371
+ stats["total_count"] = sum(
1372
+ stats.get(k, 0) for k in stats if k.endswith("_count")
1373
+ )
1374
+
1375
+ return stats
1376
+
1377
+ # ==================== HELPERS ====================
1378
+
1379
+ def _parse_datetime(self, value: Any) -> Optional[datetime]:
1380
+ """Parse datetime from database value."""
1381
+ if value is None:
1382
+ return None
1383
+ if isinstance(value, datetime):
1384
+ return value
1385
+ try:
1386
+ return datetime.fromisoformat(str(value).replace("Z", "+00:00"))
1387
+ except (ValueError, AttributeError):
1388
+ return None
1389
+
1390
+ def _row_to_heuristic(self, row: Dict[str, Any]) -> Heuristic:
1391
+ """Convert database row to Heuristic."""
1392
+ return Heuristic(
1393
+ id=row["id"],
1394
+ agent=row["agent"],
1395
+ project_id=row["project_id"],
1396
+ condition=row["condition"],
1397
+ strategy=row["strategy"],
1398
+ confidence=row["confidence"] or 0.0,
1399
+ occurrence_count=row["occurrence_count"] or 0,
1400
+ success_count=row["success_count"] or 0,
1401
+ last_validated=self._parse_datetime(row["last_validated"])
1402
+ or datetime.now(timezone.utc),
1403
+ created_at=self._parse_datetime(row["created_at"])
1404
+ or datetime.now(timezone.utc),
1405
+ embedding=self._embedding_from_db(row.get("embedding")),
1406
+ metadata=row["metadata"] if row["metadata"] else {},
1407
+ )
1408
+
1409
+ def _row_to_outcome(self, row: Dict[str, Any]) -> Outcome:
1410
+ """Convert database row to Outcome."""
1411
+ return Outcome(
1412
+ id=row["id"],
1413
+ agent=row["agent"],
1414
+ project_id=row["project_id"],
1415
+ task_type=row["task_type"] or "general",
1416
+ task_description=row["task_description"],
1417
+ success=bool(row["success"]),
1418
+ strategy_used=row["strategy_used"] or "",
1419
+ duration_ms=row["duration_ms"],
1420
+ error_message=row["error_message"],
1421
+ user_feedback=row["user_feedback"],
1422
+ timestamp=self._parse_datetime(row["timestamp"])
1423
+ or datetime.now(timezone.utc),
1424
+ embedding=self._embedding_from_db(row.get("embedding")),
1425
+ metadata=row["metadata"] if row["metadata"] else {},
1426
+ )
1427
+
1428
+ def _row_to_preference(self, row: Dict[str, Any]) -> UserPreference:
1429
+ """Convert database row to UserPreference."""
1430
+ return UserPreference(
1431
+ id=row["id"],
1432
+ user_id=row["user_id"],
1433
+ category=row["category"] or "general",
1434
+ preference=row["preference"],
1435
+ source=row["source"] or "unknown",
1436
+ confidence=row["confidence"] or 1.0,
1437
+ timestamp=self._parse_datetime(row["timestamp"])
1438
+ or datetime.now(timezone.utc),
1439
+ metadata=row["metadata"] if row["metadata"] else {},
1440
+ )
1441
+
1442
+ def _row_to_domain_knowledge(self, row: Dict[str, Any]) -> DomainKnowledge:
1443
+ """Convert database row to DomainKnowledge."""
1444
+ return DomainKnowledge(
1445
+ id=row["id"],
1446
+ agent=row["agent"],
1447
+ project_id=row["project_id"],
1448
+ domain=row["domain"] or "general",
1449
+ fact=row["fact"],
1450
+ source=row["source"] or "unknown",
1451
+ confidence=row["confidence"] or 1.0,
1452
+ last_verified=self._parse_datetime(row["last_verified"])
1453
+ or datetime.now(timezone.utc),
1454
+ embedding=self._embedding_from_db(row.get("embedding")),
1455
+ metadata=row["metadata"] if row["metadata"] else {},
1456
+ )
1457
+
1458
+ def _row_to_anti_pattern(self, row: Dict[str, Any]) -> AntiPattern:
1459
+ """Convert database row to AntiPattern."""
1460
+ return AntiPattern(
1461
+ id=row["id"],
1462
+ agent=row["agent"],
1463
+ project_id=row["project_id"],
1464
+ pattern=row["pattern"],
1465
+ why_bad=row["why_bad"] or "",
1466
+ better_alternative=row["better_alternative"] or "",
1467
+ occurrence_count=row["occurrence_count"] or 1,
1468
+ last_seen=self._parse_datetime(row["last_seen"])
1469
+ or datetime.now(timezone.utc),
1470
+ created_at=self._parse_datetime(row["created_at"])
1471
+ or datetime.now(timezone.utc),
1472
+ embedding=self._embedding_from_db(row.get("embedding")),
1473
+ metadata=row["metadata"] if row["metadata"] else {},
1474
+ )
1475
+
1476
+ def close(self):
1477
+ """Close connection pool."""
1478
+ if self._pool:
1479
+ self._pool.close()
1480
+
1481
+ # ==================== MIGRATION SUPPORT ====================
1482
+
1483
+ def _get_version_store(self):
1484
+ """Get or create the version store."""
1485
+ if self._version_store is None:
1486
+ from alma.storage.migrations.version_stores import PostgreSQLVersionStore
1487
+
1488
+ self._version_store = PostgreSQLVersionStore(self._pool, self.schema)
1489
+ return self._version_store
1490
+
1491
+ def _get_migration_runner(self):
1492
+ """Get or create the migration runner."""
1493
+ if self._migration_runner is None:
1494
+ from alma.storage.migrations.runner import MigrationRunner
1495
+ from alma.storage.migrations.versions import v1_0_0 # noqa: F401
1496
+
1497
+ self._migration_runner = MigrationRunner(
1498
+ version_store=self._get_version_store(),
1499
+ backend="postgresql",
1500
+ )
1501
+ return self._migration_runner
1502
+
1503
+ def _ensure_migrated(self) -> None:
1504
+ """Ensure database is migrated to latest version."""
1505
+ runner = self._get_migration_runner()
1506
+ if runner.needs_migration():
1507
+ with self._get_connection() as conn:
1508
+ applied = runner.migrate(conn)
1509
+ if applied:
1510
+ logger.info(f"Applied {len(applied)} migrations: {applied}")
1511
+
1512
+ def get_schema_version(self) -> Optional[str]:
1513
+ """Get the current schema version."""
1514
+ return self._get_version_store().get_current_version()
1515
+
1516
+ def get_migration_status(self) -> Dict[str, Any]:
1517
+ """Get migration status information."""
1518
+ runner = self._get_migration_runner()
1519
+ status = runner.get_status()
1520
+ status["migration_supported"] = True
1521
+ return status
1522
+
1523
+ def migrate(
1524
+ self,
1525
+ target_version: Optional[str] = None,
1526
+ dry_run: bool = False,
1527
+ ) -> List[str]:
1528
+ """
1529
+ Apply pending schema migrations.
1530
+
1531
+ Args:
1532
+ target_version: Optional target version (applies all if not specified)
1533
+ dry_run: If True, show what would be done without making changes
1534
+
1535
+ Returns:
1536
+ List of applied migration versions
1537
+ """
1538
+ runner = self._get_migration_runner()
1539
+ with self._get_connection() as conn:
1540
+ return runner.migrate(conn, target_version=target_version, dry_run=dry_run)
1541
+
1542
+ def rollback(
1543
+ self,
1544
+ target_version: str,
1545
+ dry_run: bool = False,
1546
+ ) -> List[str]:
1547
+ """
1548
+ Roll back schema to a previous version.
1549
+
1550
+ Args:
1551
+ target_version: Version to roll back to
1552
+ dry_run: If True, show what would be done without making changes
1553
+
1554
+ Returns:
1555
+ List of rolled back migration versions
1556
+ """
1557
+ runner = self._get_migration_runner()
1558
+ with self._get_connection() as conn:
1559
+ return runner.rollback(conn, target_version=target_version, dry_run=dry_run)