byterover-cli
byterover-cli copied to clipboard
Architecture for Multi-Backend Persistent Conversation History in Cipher
Architecture for Multi-Backend Conversation History in Cipher
1. Title and Overview
Title:
Multi-Backend Redundant Conversation History for Cipher
Overview:
This architecture enables Cipher to persist conversation history across multiple storage backends (e.g., SQLite, PostgreSQL, in-memory) for redundancy, reliability, and production-readiness. All writes are attempted on all configured backends; reads are served from the first available backend with data.
2. Guiding Principles / Design Philosophy
- Reliability & Redundancy: Data is written to multiple backends to prevent loss from single-point failures.
- Extensibility: New backend types (e.g., S3, Redis) can be added with minimal changes.
- Backward Compatibility: Existing single-backend configs continue to work.
- Separation of Concerns: Backend-specific logic is encapsulated; coordination is handled by a manager.
- Observability: Failures and fallback events are logged and exposed for monitoring.
- Performance Awareness: Writes are parallelized/asynchronous where possible to minimize latency.
3. High-Level Architecture Diagram (Conceptual)
flowchart TD
subgraph User/Agent
A[Cipher Core]
end
subgraph Backend Manager
B[HistoryBackendManager]
end
subgraph Backends
C1[SQLite Backend]
C2[Postgres Backend]
C3[In-Memory Backend]
end
A -- read/write --> B
B -- write --> C1
B -- write --> C2
B -- write --> C3
B -- read (priority order) --> C1
B -- read (fallback) --> C2
B -- read (fallback) --> C3
Explanation:
Cipher Coreinteracts only with theHistoryBackendManager.- The manager coordinates all read/write operations across the configured backends.
- Writes are attempted on all; reads are attempted in order until data is found.
4. Detailed Component Breakdown
4.1. IHistoryBackend Interface
- Location:
src/core/storage/backend/types.ts - Role: Contract for all backend implementations.
- Responsibilities:
writeHistory(sessionId, message)readHistory(sessionId)healthCheck()
- Interfaces/APIs: Exposed to backend manager.
- Dependencies: None (pure interface).
- Impact: All backends must implement this.
4.2. Backend Implementations
- Location:
- SQLite:
src/core/storage/backend/sqlite.ts - Postgres:
src/core/storage/backend/postgresql.ts - In-Memory:
src/core/storage/backend/in-memory.ts
- SQLite:
- Role: Persist and retrieve conversation history.
- Responsibilities: Implement
IHistoryBackend. - Dependencies: Database drivers, config.
4.3. HistoryBackendManager
- Location:
src/core/storage/manager.ts(or new file) - Role: Orchestrates multi-backend logic.
- Responsibilities:
- Loads backend configs from
memAgent/cipher.yml. - Instantiates all specified backends.
- On
write: attempts to write to all backends (logs failures). - On
read: queries backends in order, returns first successful result. - Exposes health/status of each backend.
- Loads backend configs from
- Interfaces/APIs:
writeHistory(sessionId, message)readHistory(sessionId)
- Dependencies: All backend implementations, config loader.
- Impact: Centralizes multi-backend logic.
4.4. Config Loader/Validator
- Location:
src/core/storage/config.ts(or similar) - Role: Parses and validates backend config.
- Responsibilities:
- Supports both single and multi-backend formats.
- Validates required fields for each backend type.
- Dependencies: YAML parser, schema validation.
4.5. Logging & Monitoring
- Location:
src/core/logger/ - Role: Logs backend failures, fallback events, and exposes metrics.
- Responsibilities:
- Log write/read errors per backend.
- Expose health status for observability.
5. Key Interactions and Data Flow (Walkthroughs)
5.1. Write Flow
Cipher CorecallsHistoryBackendManager.writeHistory(sessionId, message).- Manager attempts to write to all configured backends in parallel.
- If any backend fails, logs the error and continues.
- Returns success if at least one backend succeeds (configurable).
5.2. Read Flow
Cipher CorecallsHistoryBackendManager.readHistory(sessionId).- Manager queries backends in configured order.
- Returns the first successful result.
- If all fail, returns error and logs incident.
5.3. Health Check
- Manager periodically checks health of all backends and logs/report status.
6. Cross-Cutting Concerns / Non-Functional Requirements
-
Error Handling:
- All backend errors are logged with context.
- Optionally, retries for transient errors.
- Alerts if all backends fail.
-
Configuration Management:
- Supports both legacy and new config formats.
- Validates config at startup.
-
Scalability:
- Designed to add/remove backends with minimal code changes.
-
Security:
- Sensitive config (e.g., DB credentials) handled via environment variables.
-
Observability:
- Logs all backend operations and failures.
- Exposes metrics for monitoring.
-
Testing Strategy:
- Unit tests for manager and backends.
- Integration tests for multi-backend scenarios.
- Migration tests for config changes.
7. Implementation Guidelines / Best Practices
- Follow existing interface and factory patterns for new backends.
- Use async/await for parallel writes to minimize latency.
- Ensure all errors are caught and logged; never crash on single-backend failure.
- Write clear, user-facing error messages for config issues.
- Maintain backward compatibility with single-backend configs.
- Document all new config options and migration steps.
8. Impacted Files / Modules Summary
memAgent/cipher.yml(config changes)src/core/storage/backend/types.ts(interface updates)src/core/storage/backend/sqlite.ts(ensure compliance)src/core/storage/backend/postgresql.ts(ensure compliance)src/core/storage/backend/in-memory.ts(ensure compliance)src/core/storage/manager.ts(new or updated for multi-backend logic)src/core/storage/config.ts(config loader/validator)src/core/logger/(logging enhancements)- Tests:
src/core/storage/__test__/
9. Definition of Done / Success Criteria
- Cipher can be configured with multiple history backends via
memAgent/cipher.yml. - All conversation history writes are attempted on all configured backends.
- Reads are served from the first available backend with data.
- Failures in one backend do not prevent operation if others are available.
- All errors and fallback events are logged.
- Backward compatibility with single-backend configs is maintained.
- Documentation and tests are updated to reflect new functionality.
10. Open Questions / Future Considerations
- Should there be a configurable policy for what constitutes a "successful" write (e.g., quorum, all, any)?
- Should backend health be exposed via an API for external monitoring?
- Is there a need for eventual consistency or reconciliation if backends become out of sync?
- Should we support dynamic reloading of backend config without restart?