Skip to content

[BUG] Database initialization fails when /var/lib/cortex/ exists but not writable & Ollama provider choice not persisted #514

@sujay-d07

Description

@sujay-d07

What happened?

Issue 1: Database Initialization Failure

When running cortex install without sudo, the application crashes with:

ERROR:cortex.installation_history:Failed to initialize database: unable to open database file
❌ Unexpected error: unable to open database file

Root Cause:
The _ensure_db_directory() method in InstallationHistory and SemanticCache classes only checks if the directory /var/lib/cortex/ can be created, but not if the user can actually write files inside it.
When /var/lib/cortex/ already exists (created by a previous sudo operation), mkdir(..., exist_ok=True) succeeds silently without raising PermissionError. The fallback to ~/.cortex/ never triggers, and SQLite fails when trying to create the database file.

Issue 2: Ollama Provider Choice Not Persisted

When a user selects Ollama as their provider (option 3), the choice is not saved. Every subsequent cortex command prompts the user to select a provider again.
Root Cause:
The prompt_for_key() method returns immediately for Ollama without offering to save the preference, unlike the behavior for API keys.

How to reproduce

Issue 1: Database Error

  1. Ensure /var/lib/cortex/ exists and is owned by root:
    sudo mkdir -p /var/lib/cortex ls -la /var/lib/cortex/ # Should show: drwxr-xr-x root root
  2. Run cortex as a non-root user:
    cortex install nginx
  3. Select Ollama (option 3) when prompted for provider then it will crash with "unable to open database file"

Issue 2: Ollama Provider Not Saved

  1. Remove any existing config:
    rm -f ~/.cortex/.env
  2. Run cortex and select Ollama:
    cortex install nginx --dry-run # Select option 3 (Ollama)
  3. Run cortex again:
    cortex install docker --dry-run
    This will remember Ollama preference, no prompt shown

Environment

  • OS: Ubuntu 22.04+ / Debian 12+ (or any Linux distribution)
  • Python: 3.10+
  • User: Non-root user
  • Condition: /var/lib/cortex/ directory exists (created by previous root/sudo operation)
  • Ollama: Installed and running locally (for provider persistence issue)

Error output

Image

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions