6.7 KiB
6.7 KiB
Memory Architecture Roadmap (2025-10-15)
Current Snapshot
PgDalnow prefers Qt6/QSql (QPSQL) with an in-memory fallback forstub://DSNs; schema migrations live indb/init/.kompanion --initguides DSN detection (psql socket probing), applies migrations, and persists config via~/.config/kompanionrc.- MCP handlers still parse JSON manually but leverage the shared DAL; resource descriptors under
resources/memory/kom.memory.v1/capture episodic/semantic contracts. - Contract tests (
contract_memory,contract_mcp_tools,mcp_memory_exchange) validate the Qt-backed DAL and MCP handlers.
1. CTest Target: contract_memory
- Keep
contract_memory.cppfocused on exercisingPgDalwrite/read surfaces; expand as DAL features land. - Ensure the executable runs without Postgres by defaulting to
stub://memorywhenPG_DSNis absent. - Layer follow-up assertions once the QSql path is exercised end-to-end (CI can target the packaged test database).
2. DAL (Qt6/QSql) Evolution
Dependencies
- Qt6 (Core, Sql) with the
QPSQLdriver available at runtime. - KDE Frameworks
ConfigCorefor persisting DSNs inkompanionrc.
Implementation Steps
- Parse libpq-style DSNs with
QUrl, openQSqlDatabaseconnections when the DSN is notstub://, and maintain the existing in-memory fallback for tests. - Use
QSqlQueryINSERT ... RETURNINGstatements for namespaces, items, chunks, and embeddings; emit vector literals ([0.1,0.2]) when targeting pgvector columns. - Surface detailed
QSqlErrormessages (throwingstd::runtime_error) so MCP handlers and the CLI can report actionable failures. - Share configuration between CLI and MCP runners via KConfig (
Database/PgDsn), seeded through the newkompanion --initwizard.
3. MCP resources/* & Episodic→Semantic Sync
Directory Layout
- Create
resources/memory/kom.memory.v1/for tool descriptors and schema fragments:episodic.json– raw conversation timeline.semantic.json– chunked embeddings metadata.jobs/semantic_sync.json– background job contract.
Design Highlights
- Episodic resource fields:
namespace,thread_id,speaker,content,sensitivity,tags,created_at. - Semantic resource references episodic items (
episodic_id,chunk_id,model,dim,vector_ref). - DAL sync job flow:
- Locate episodic rows with
embedding_status='pending'(andsensitivity!='secret'). - Batch call embedder(s); write
memory_chunks+embeddings. - Mark episodic rows as
embedding_status='done', capture audit entries (e.g., ledger append).
- Locate episodic rows with
- Expose a placeholder MCP tool
kom.memory.v1.sync_semanticthat enqueues or executes the job. - Note TTL and privacy requirements; skip items with
expires_atin the past or flagged secret.
Ξlope Alignment Notes (2025-10-15)
- Episodic resources capture resonance links and identity hints so the Librarian layer (see
elope/doc/architecture_memory.md) can strengthen cross-agent patterns without raw content sharing. - Semantic resources surface
identity_vectorandsemantic_weight, enabling supersemantic indexing once crystallization occurs. jobs/semantic_syncmaintainscursor_event_idand skipssensitivity=secret, mirroring the elope crystallization guidance in/tmp/mem-elope.txt.
4. hybrid_search_v1 with pgvector
SQL Components
- Update migrations (
sql/pg/001_init.sql) to include:tsvectorgenerated column or expression for lexical search.GINindex on the lexical field (eitherto_tsvectororpg_trgm).- Per-model
ivfflatindex onembeddings.vector.
- Prepared statements:
- Text:
SELECT id, ts_rank_cd(...) AS score FROM memory_items ... WHERE namespace_id=$1 AND text_query=$2 LIMIT $3. - Vector:
SELECT item_id, 1 - (vector <=> $2::vector) AS score FROM embeddings ... WHERE namespace_id=$1 ORDER BY vector <-> $2 LIMIT $3.
- Text:
- Merge results in C++ with Reciprocal Rank Fusion or weighted sum, ensuring deterministic ordering on ties.
Handler Integration
- Ensure
PgDal::hybridSearchdelegates to SQL-based lexical/vector search when a database connection is active, reusing the in-memory fallback only forstub://. - Return richer matches (id, score, optional chunk text) to satisfy MCP response schema.
- Update
HandlersMemory::search_memoryto surface the new scores and annotate whether lexical/vector contributed (optional metadata). - Exercise hybrid queries in contract tests against the packaged test database (
db/scripts/create-test-db.sh).
5. Secret Handling, Snapshots, and CLI Hooks
- Secret propagation: episodic
sensitivity+embeddableflags gate embedding generation. DAL queries will add predicates (metadata->>'sensitivity' != 'secret') before hybrid search. - Snapshots: episodic entries with
content_type = snapshotreference durable artifacts; sync summarises them into semantic text while retainingsnapshot_reffor CLI inspection. - Hybrid policy:
pgSearchVectorwill filter by caller capability (namespace scope, secret clearance) before ranking; contract tests must assert omission of secret-tagged items. - CLI sketch: plan for a Qt
QCoreApplicationtool (kom_mctl) exposing commands to list namespaces, tail episodic streams, triggersync_semantic, and inspect resonance graphs—all wired through the new prepared statements. - Observability: CLI should read the
jobs/semantic_syncstate block to display cursors, pending counts, and last error logs; dry-run mode estimates embeddings without committing. - Activation parity: Long term, mirror the KDE
akonadiclient/akonadi-consolepattern—Kompanion CLI doubles as an MCP surface today and later as a DBus-activated helper so tools can be socket-triggered into the memory service. - KConfig defaults:
kom_mcpandkompanionloadDatabase/PgDsnfrom~/.config/kompanionrc(seedocs/configuration.md) whenPG_DSNis unset, keeping deployments kioskable. - CLI UX:
kompanion --initguides first-run setup (auto-detects databases, applies schemas);-I/--interactivekeeps a JSON REPL open, and-V/--verboseechoes request/response streams for future HTTP transport parity.
Next-Step Checklist
- Promote Qt6/QSql backend (QPSQL) as default DAL; retain
stub://fallback for tests. - Normalize contract_memory CTest target and remove stale library target.
- Author
resources/memory/descriptors and sync job outline. - Extend DAL header to expose richer query structs (filters, pagination, secret handling).
- Update
docs/mcp-memory-api.mdto mention episodic sync + hybrid search fields. - Create follow-up acf subtasks when concrete implementation begins (pgvector migration, scheduler hook, runtime wiring).