byterover-cli icon indicating copy to clipboard operation
byterover-cli copied to clipboard

[BUG] SessionManager ignores STORAGE_DATABASE_TYPE and STORAGE_DATABASE_PATH

Open Maxakali517 opened this issue 1 year ago • 4 comments

Bug Description

SessionManager has two issues with environment variable handling:

  1. Ignores STORAGE_DATABASE_TYPE environment variable: Even when explicitly set to in-memory, SessionManager always attempts to create SQLite database first, causing unnecessary errors and confusion.

  2. Ignores STORAGE_DATABASE_PATH environment variable: When SQLite is desired, the path specified in STORAGE_DATABASE_PATH is completely ignored, and a hardcoded path ./data/cipher-sessions.db is always used instead.

These issues cause permission errors in containerized environments and prevent proper configuration of storage backends.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create a docker-compose.yml in the project root with the following environment configuration:
services:
  cipher:
    build:
      context: .
      dockerfile: Dockerfile
    image: cipher
    ports:
      - "4000:4000"
    environment:
      MCP_SERVER_MODE: aggregator
      USE_ASK_CIPHER: "false"
      OPENAI_API_KEY: "dummy"
      STORAGE_DATABASE_TYPE: "sqlite"
      STORAGE_DATABASE_PATH: "/app/.cipher/cipher-sessions.db"

    command: >
      node dist/src/app/index.cjs
      --mode mcp
      --agent /app/memAgent/cipher.yml
      --mcp-transport-type streamable-http
      --mcp-port 4000
      --mcp-host 0.0.0.0
    volumes:
      - ./memAgent:/app/memAgent:ro
      - cipher-data:/app/.cipher
    restart: always

volumes:
  cipher-data:
  1. Build and start the Docker container:
docker-compose up --build
  1. Check the container logs:
docker compose logs
  1. See SQLite connection error despite in-memory configuration

Expected Behavior

Issue 1 - STORAGE_DATABASE_TYPE handling:

  • When set to in-memory, SessionManager should use in-memory storage directly without attempting SQLite connection
  • Should not generate any SQLite-related error logs when in-memory is configured

Issue 2 - STORAGE_DATABASE_PATH handling:

  • When set (e.g., /app/.cipher/cipher-sessions.db), SessionManager should use the specified path for SQLite database
  • Should respect the full path including directory and filename

Actual Behavior

Issue 1 - STORAGE_DATABASE_TYPE ignored:

  • Even with STORAGE_DATABASE_TYPE: "in-memory", SessionManager always attempts to create SQLite database first
  • Generates unnecessary SQLite connection errors before falling back to in-memory
  • Creates confusion in logs despite eventually working with in-memory storage

Issue 2 - STORAGE_DATABASE_PATH ignored:

  • Despite setting STORAGE_DATABASE_PATH: "/app/.cipher/cipher-sessions.db", SessionManager uses hardcoded ./data/cipher-sessions.db
  • Results in permission errors when the hardcoded path is not writable
  • Makes it impossible to configure custom database locations

Screenshots

Not applicable for this issue.

Environment Information

  • OS: Linux (Docker container)
  • Browser: N/A (server-side issue)
  • Version: Cipher v0.3.0
  • Node.js version: v20.18.1
  • Docker base image: node:20.18.1-alpine
  • Mode: MCP aggregator
  • Transport: streamable-http

Logs and Error Messages

Failed to connect to SQLite database {
  path: 'data/cipher-sessions.db',
  directory: 'data',
  directoryExists: false,
  error: "EACCES: permission denied, mkdir 'data'",
  stack: "Error: EACCES: permission denied, mkdir 'data'\n" +
    '    at async mkdir (node:internal/fs/promises:858:10)\n' +
    '    at async SqliteBackend.connect (/app/dist/src/app/index.cjs:21059:13)\n' +
    '    at async _StorageManager.connect (/app/dist/src/app/index.cjs:21898:13)\n' +
    '    at async SessionManager.initializeSqliteStorage (/app/dist/src/app/index.cjs:24156:11)\n' +
    '    at async SessionManager.initializePersistenceStorage (/app/dist/src/app/index.cjs:24129:11)\n' +
    '    at async SessionManager.init (/app/dist/src/app/index.cjs:23524:9)\n' +
    '    at async createAgentServices (/app/dist/src/app/index.cjs:50564:3)\n' +
    '    at async MemAgent.start (/app/dist/src/app/index.cjs:51984:20)',
  nodeVersion: 'v20.18.1',
  platform: 'linux',
  arch: 'x64'
}
05:18:31 ERROR: Failed to connect to database backend
05:18:31 WARN: [StorageManager:Database] Primary backend failed, attempting fallback to in-memory

Additional Context

The issue occurs in src/core/session/session-manager.ts in the initializeSqliteStorage() method, which hardcodes the SQLite configuration:

private async initializeSqliteStorage(): Promise<void> {
    try {
        // Try SQLite as fallback
        logger.debug('SessionManager: Attempting to initialize SQLite storage...');
        this.storageManager = new StorageManager({
            database: {
                type: 'sqlite',
                path: './data',  // <-- Hardcoded path
                database: 'cipher-sessions.db',  // <-- Hardcoded filename
            },
            cache: {
                type: 'in-memory',
            },
        });

These issues particularly affect:

  • Issue 1: Stateless MCP relay servers that don't need persistent storage but get SQLite errors
  • Issue 2: Docker deployments that need custom database paths due to volume mounts
  • Both: Container environments with restricted file permissions where hardcoded paths fail

Possible Solution

Modify initializeSqliteStorage() to check environment variables before attempting SQLite initialization:

private async initializeSqliteStorage(): Promise<void> {
    try {
        // Check if we should use in-memory storage based on environment variable
        if (env.STORAGE_DATABASE_TYPE === 'in-memory') {
            logger.debug('SessionManager: Using in-memory storage as configured');
            this.storageManager = new StorageManager({
                database: {
                    type: 'in-memory',
                },
                cache: {
                    type: 'in-memory',
                },
            });
            await this.storageManager.connect();
            logger.info('SessionManager: In-memory storage initialized successfully');
            return;
        }

        // Use environment variables for SQLite configuration
        const dbPath = env.STORAGE_DATABASE_PATH;
        let sqlitePath = './data';
        let sqliteDatabase = 'cipher-sessions.db';

        if (dbPath) {
            const pathParts = dbPath.split('/');
            sqliteDatabase = pathParts.pop() || 'cipher-sessions.db';
            sqlitePath = pathParts.join('/') || './data';
        }

        this.storageManager = new StorageManager({
            database: {
                type: 'sqlite',
                path: sqlitePath,
                database: sqliteDatabase,
            },
            cache: {
                type: 'in-memory',
            },
        });
        // ... continue with SQLite initialization

This solution:

  1. Checks STORAGE_DATABASE_TYPE environment variable first
  2. Uses STORAGE_DATABASE_PATH for SQLite path configuration
  3. Skips SQLite initialization when in-memory storage is explicitly configured

Checklist

  • [x] I have searched for similar issues before creating this one
  • [x] I have provided all the requested information above
  • [x] I have tested this on the latest version

Maxakali517 avatar Sep 13 '25 06:09 Maxakali517

I'll have a look at that. Stay stuned!

RyanNg1403 avatar Sep 16 '25 16:09 RyanNg1403

Thank you for letting us know the bug. I just resolve it in #259

Please pull the code again to update your codebase

willingWill17 avatar Sep 17 '25 13:09 willingWill17

Thank you so much for fixing this issue. I really appreciate it! 🙇‍♂️🙇‍♂️🙇‍♂️

Maxakali517 avatar Sep 17 '25 14:09 Maxakali517

Glad I could help. I hope you’ll continue supporting the team’s work in the future.

willingWill17 avatar Sep 17 '25 14:09 willingWill17